Skip to content

Instantly share code, notes, and snippets.

@thedamon
Created February 28, 2014 17:59
Show Gist options
  • Star 25 You must be signed in to star a gist
  • Fork 3 You must be signed in to fork a gist
  • Save thedamon/9276193 to your computer and use it in GitHub Desktop.
Save thedamon/9276193 to your computer and use it in GitHub Desktop.
Cause back button to close Bootstrap modal windows
$('div.modal').on('show', function() {
var modal = this;
var hash = modal.id;
window.location.hash = hash;
window.onhashchange = function() {
if (!location.hash){
$(modal).modal('hide');
}
}
});
$('div.modal').on('hide', function() {
var hash = this.id;
history.pushState('', document.title, window.location.pathname);
});
@bjesus
Copy link

bjesus commented Aug 30, 2014

works with Bootstrap 3 by changing "show" to "show.bs.modal" and "hide" to "hidden.bs.modal"

@hubert17
Copy link

hubert17 commented Sep 9, 2014

It works on me by changing only the "show" to "show.bs.modal"
Retain "hide" as "hide"
(Bootsrap 3.2.0)

@zdravimte
Copy link

after pressing the back button and making the modal dissappear (which works great), is there a way how to prevent the possibility of going forward again? (the URL with the hash = modalDiv is still there in the history tracklist)

@nhtahoe
Copy link

nhtahoe commented Mar 22, 2015

This seemed to work for me:

 $(".modal").on("shown.bs.modal", function()  { // any time a modal is shown
    var urlReplace = "#" + $(this).attr('id'); // make the hash the id of the modal shown
    history.pushState(null, null, urlReplace); // push state that hash into the url
  });

  // If a pushstate has previously happened and the back button is clicked, hide any modals.
  $(window).on('popstate', function() { 
    $(".modal").modal('hide');
  });

@nikopolv
Copy link

Can't get this to trigger the event "shown.bs.modal". What could be wrong. I'm using Bootstrap v3.3.2.

e: show.bs.modal works.

@thsl
Copy link

thsl commented Jun 18, 2015

The nhtahoe code works fine! I've tested in my phone too, and works too!

@mismith
Copy link

mismith commented Aug 4, 2015

Thanks all!

@rioadrian
Copy link

@nhtahoe thanks for solutions, it works for me 👍

@arnorhs
Copy link

arnorhs commented May 1, 2016

I don't believe this solution accounts for the user closing the modal without eg. pressing back. if you click a dismiss button or press escape etc, the modal will get hidden but the url has not updated.

@willamesoares
Copy link

willamesoares commented Dec 16, 2016

There is actually a bug with these solutions and I'm currently struggling to debug in order to find the problem. When you click to open the modal multiple times and then close it without using the back button, the history keeps track of each state that is being added by pushState but is not being popped. With that you would have to click multiple times in the back button to really go back to the previous page after you close the modal.

@tdsymonds
Copy link

Thanks for this, was really helpful.

However, I've managed to solve the problem that @willamesoares has mentioned in my fork:

https://gist.github.com/tdsymonds/23917215f591a9e1442a38783c77f0f0

Hope this helps too!

@Angel-Glez
Copy link

This seemed to work for me:

 $(".modal").on("shown.bs.modal", function()  { // any time a modal is shown
    var urlReplace = "#" + $(this).attr('id'); // make the hash the id of the modal shown
    history.pushState(null, null, urlReplace); // push state that hash into the url
  });

  // If a pushstate has previously happened and the back button is clicked, hide any modals.
  $(window).on('popstate', function() { 
    $(".modal").modal('hide');
  });

It works fine for me too but I had the problem with 2 or more visible modals in same page, when I wanted to close the top modal, all modal get close, so I changed a little bit the $(window).on('popstate', function() function and fully works.
This is my first conttribution, hope this helps.

  $(window).on('popstate', function() { 
     //$(".modal").modal('hide');
     if ($(".modal:visible").length){                                 // validate that there are visible modal
         $(".modal").eq($(".modal:visible").length-1).modal('hide');  // hide only the last model opened
     }
  });```

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment