Conditionally open popup video based on URL query string

这一生的挚爱 提交于 2019-12-24 21:18:01

问题


I have a page (somepage.aspx) that has a popup video on it. The video opens when a link is clicked using the js $('.showVideo').live('click', function() {

I have another page (otherpage.aspx) and would like to link to /somepage.aspx with some kind of URL parameter that automatically opens the video popup. Something like /somepage.aspx?video=1 ... based on the url parameter the video would open. How would I add this to my existing js?

Thanks


回答1:


Using this function you are able to detect the presence of ?video=1 in the url:

function getURLParameter(name) {
    return decodeURI(
        (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
    );
}

Source: Get escaped URL parameter Credit to https://stackoverflow.com/users/726427/pauloppenheim

Then you could do something like:

if(getURLParameter('video')==1){
  $(".showVideo").trigger('click');
}

edit:

$(document).ready(function(){               


    function getURLParameter(name) {
        return decodeURI(
            (RegExp(name + '=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
        );
    }
    if(getURLParameter('video')==1){
      $(".showVideo").trigger('click');
    }
});

Wrap the parameter name(video) in quotes getURLParameter('video').

Another Edit

Wrap your click event handler in a function, basically remove everything form:

$('.showVideo').live('click', function() {
    //build overlay
    (...)
    return false;
});

Cut&paste it inside a function. Then just call the function from inside:

$('.showVideo').live('click', function() {
    my_function();
});

Then change the previous code to:

if(getURLParameter('video')==1){
     my_function()
}


来源:https://stackoverflow.com/questions/13746416/conditionally-open-popup-video-based-on-url-query-string

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!