Skip to content

Instantly share code, notes, and snippets.

@thedamon
Created February 28, 2014 17:59
Show Gist options
  • 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);
});
@lethanhnguyen123
Copy link

lethanhnguyen123 commented Aug 1, 2024

Please help me
And here is my modal it is not working bootstrap 3.2.0

### <a href="" data-toggle="modal" data-target="#exampleModal">Mô Tả MP3</a>
<!-- Modal -->
<div class="modal fade" id="exampleModal" tabindex="-1" role="dialog" aria-labelledby="exampleModalLabel" aria-hidden="true">
  <div class="modal-dialog<?php if(mobile()){echo'-centered';}?> modla-lg" role="document">
    <div class="modal-content">
      <div class="modal-header">
        <h5 class="modal-title" id="exampleModalLabel">ஜ۩۞۩ஜ Description ஜ۩۞۩ஜ</h5>
        <button type="button" class="close" data-dismiss="modal" aria-label="Close">
          <span aria-hidden="true">&times;</span>
        </button>
      </div>
      <div class="modal-body">
	  description...........
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-secondary" data-dismiss="modal">Close</button>
      </div>
    </div>
  </div>
</div>

<script type="text/javascript">
$('div.modal').on('show.bs.modal', 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);
 });
</script>

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