Skip to content Skip to sidebar Skip to footer

Conditionally Open Popup Video Based On Url Query String

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 (othe

Solution 1:

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

functiongetURLParameter(name) {
    returndecodeURI(
        (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(){               


    functiongetURLParameter(name) {
        returndecodeURI(
            (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
    (...)
    returnfalse;
});

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()
}

Post a Comment for "Conditionally Open Popup Video Based On Url Query String"