根据URL查询字符串有条件地打开弹出视频 [英] Conditionally open popup video based on URL query string

查看:93
本文介绍了根据URL查询字符串有条件地打开弹出视频的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个页面(somepage.aspx),上面有一个弹出视频.当使用js $('.showVideo').live('click', function() {

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

我有另一个页面(otherpage.aspx),并希望使用某种URL参数链接到/somepage.aspx,该参数会自动打开视频弹出窗口.像/somepage.aspx?video=1 ...这样的东西会基于url参数打开视频.如何将其添加到现有的js中?

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?

谢谢

推荐答案

使用此功能,您可以检测到URL中?video=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]
    );
}

来源:获取转义的URL参数 归功于 https://stackoverflow.com/users/726427/pauloppenheim

然后您可以执行以下操作:

Then you could do something like:

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

$(document).ready(function(){               


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

将参数名称(视频)括在引号getURLParameter('video')中.

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

另一个编辑

将click事件处理程序包装在一个函数中,基本上删除所有形式:

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

这篇关于根据URL查询字符串有条件地打开弹出视频的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆