如何使用直接链接到模态窗口

时间:2014-11-20 17:44:59

标签: jquery bootstrap-modal

我想使用以下代码:

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

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

如何将其与以下Modal示例一起使用:

<a href="#" data-target=".bs-nps-modal" data-toggle="modal">

2 个答案:

答案 0 :(得分:0)

http://jsfiddle.net/kge5rqv3/4/

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

function popModal()
{
    $('#modal1').modal('show');
}

新JS: http://jsfiddle.net/kge5rqv3/7/

var id= getUrlParameter('id');

if (id == 'modal1') {
     popModal();
}

function popModal()
{
    $('#modal1').modal('show');
}


function getUrlParameter(sParam)
{

    var sPageURL = window.location.search.substring(1);    

    //simulate url http://example.com?id=modal1
    //remove when implementing this code
    var sPageURL='id=modal1';

    var sURLVariables = sPageURL.split('&');
    for (var i = 0; i < sURLVariables.length; i++) 
    {
        var sParameterName = sURLVariables[i].split('=');
        if (sParameterName[0] == sParam) 
        {
            return sParameterName[1];
        }
    }
} 

答案 1 :(得分:0)

我对已接受的解决方案存在一些问题,需要对其进行调试。

我认为这是一个简化的实现。

 function popModal()
{
    $('#modaltopop').modal('show');
}

function getUrlParameter(sParam)
{
    return window.location.search.substring(1);
}

var id= getUrlParameter('link_parameter'); 

if (id == 'link_parameter') {
    popModal();
}


<a href='/?link_parameter'>click me</a>

只要您的链接参数与&#34; getUrlParameter(&#39; link_parameter&#39;)&#34;中的参数相匹配,

samplurl.com/?link_parameter