The Garrus The Garrus -4 years ago 110
Javascript Question

Close modal by clicking BROWSER back button

Currently i just have this:

```

<div data-slideid="1" class="slide">
<div class="row">
<div class="col-md-12 text-center modal-intro">
<div class="container"><img src="assets/images/work/logo1.png" width="100" height="100" alt="Logo" class="img-circle"/><span>ABOUT</span>
<h3>Some text</h3>
<p>some more text </p>
</div>
</div>
</div>


```

Basically when a button is clicked it just slides modal in on the same URL. I need # or something to be added to the URL when modal is opened so people can click back button in the browser to go back.

I have X close button and everything but a lot of people use back button to go back and i would like to have it added.

Is there a simple way of acomplishing this?

Answer Source
function ensureHash(newHash) {
    if (window.location.hash) {
        return window.location.href.substring(0, window.location.href.lastIndexOf(window.location.hash)) + newHash;
    }
    return window.location.hash + newHash
}

Use the function above to set the URL you need and do this when you call a button

window.location.href = ensureHash("#foo=bar");
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download