Conditionally open popup video based on URL query

2019-09-16 16:36发布

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条回答
你好瞎i
2楼-- · 2019-09-16 17:29

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()
}
查看更多
登录 后发表回答