Trigger Bootstrap Modal Content using Hash from URL

This script will let you show Bootstrap Modal content if you directly put the Modal ID on the url.

Example:

Let’s say if you click a link from page one, then you want show the modal content instantly on page two depending on what ID it was using.

HTML Codes
Page One:


<a href="http://www.yourdomain.com/page-two/#modalID">Click</a>

Page Two:


<div class="modal fade" id="myModal" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
...
</div>

Script

This script must be on page two in order to call the modal hash ID.



$(function(){
      if(window.location.hash) {
          var hash = window.location.hash;
          $(hash).modal('toggle');
      }
  });