Bootstrap - 直接链接到模态窗口

时间:2013-12-18 15:44:20

标签: javascript jquery twitter-bootstrap modal-dialog twitter-bootstrap-3

是否有可能有这样的网址:

www.url.com/#imprint
(或类似)直接链接到打开模式窗口的页面?

这可能吗?任何提示?
谢谢!

4 个答案:

答案 0 :(得分:6)

你可能会做这样的事情。

if (window.location.hash == "#imprint") {
     $('#myModal').modal('show');
}

答案 1 :(得分:3)

Just for future visitors/readers, I have created a very simple function to open a modal dynamically without the need to know the exact ID that you're looking for. It just falls through if no hash is located.

/**
 * Function to open a bootstrap modal based on ID
 * @param int
 */
function directLinkModal(hash) {
  $(hash).modal('show');
}

/**
 * Call the function on window load
 * @param hash of the window
 */
directLinkModal(window.location.hash);

答案 2 :(得分:2)

是的,这是可能的。只需查看网址:

function popModal() {
      // code to pop up modal dialog
    }

    var hash = window.location.hash;
    if (hash.substring(1) == 'modal1') {
      popModal();
    }

答案 3 :(得分:1)

失去了几个小时后,我想出了这个。根据在第1页点击的链接,在第2页打开不同模态的解决方案。 HTML代码基于Bootstrap官方Modal示例。

HTML |页1.HTML

<body>    
  <a href="http://yourwebsi.te/page-2.html?showmodal=1">
  <a href="http://yourwebsi.te/page-2.html?showmodal=2">
</body>

JS | ShowModal.js

$(document).ready(function() {
  var url = window.location.href;
  if (url.indexOf('?showmodal=1') != -1) {
    $("#modal-1").modal('show');
  }
  if (url.indexOf('?showmodal=2') != -1) {
    $("#modal-2").modal('show');
  }
});

HTML |页2.HTML

<body>    
  <div class="modal fade bd-example-modal-lg" id="modal-1" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">Your content</div>
    </div>
  </div>
  <div class="modal fade bd-example-modal-lg" id="modal-2" tabindex="-1" role="dialog" aria-labelledby="myLargeModalLabel" aria-hidden="true">
    <div class="modal-dialog modal-lg">
      <div class="modal-content">Your content</div>
    </div>
  </div>
  <script src="ShowModal.js"></script>
</body>