不同的colorbox调用取决于特定的cookie

时间:2014-02-28 19:19:39

标签: javascript jquery ajax cookies colorbox

我想在这里做两件事。 如果有人访问该网站并且他们的计算机上有cookie“v_regUsr”,那么当他们点击彩盒中的链接时,该计划应该会正常打开,就像我在下面的html中一样。

如果有人访问该网站并且他们没有cookie“v_regUsr”,那么在使用这些设置点击它时应该打开一个不同的颜色框窗口:

$.colorbox({width:"480px",height:"290px", iframe:true, href:"test.php"});

我的HTML:

<a href="plan.php"  class="iframeplan"><img src="planbutton.png" border="0"/></a>

我的剧本:

<script type="text/javascript">
if (document.cookie.indexOf("v_regUsr") >= 0) {
  // They've been here before.

}
else {
  //They've not been here before.
   $.colorbox({width:"480px",height:"290px", iframe:true, href:"test.php"});
}
</script>

这可能吗?

1 个答案:

答案 0 :(得分:0)

我倾向于使用jQuery:

if($.cookie('v_regUsr')){
    //the cookie exists
    $('a#galleryLink').attr('href', 'plan.php'); //EDIT - adds an attribute to any <a> tag with the id 'galleryLink'
    jQuery('a.gallery').colorbox({/*insert your key:value paired options specific to established users here*/});
} else{
    //the cookie does not exist
    $('a#galleryLink').attr('href', 'test.php'); //EDIT - as above, but for test.php
    jQuery('a.gallery').colorbox({/*insert your key:value paired options specific to new users here*/});
}

这只是检查cookie的存在。 if()语句中的脚本来自colorbox网站,所有键:值对都是here

上面的答案假设您在html中使用了一些带有公共类的<a>标记,在本例中为gallery


编辑:

请参阅上面的代码注释以进行更改。

这假设您为每个链接添加id=""属性,每个元素的ID必须相同,这不是理想的,但前提是您不需要使用ID来引用个体实体,那么它就不会破裂。

也就是说,看起来你可以省略link标签中的href=""属性,并允许colorbox API为你处理这个问题 - 你在自己的演示代码{{1}中有了正确的想法我自己没有在colorbox中使用过这个,所以无法证明它有效。


编辑2:

href:"test.php"

编辑3:

if($.cookie('v_regUsr')){
    //the cookie exists
    $('#linkOne').attr('href', 'plan1.php'); //EDIT - this assigns an attribute to an element with the ID of 'linkOne'
    $('#linkTwo').attr('href', 'plan2.php'); //as above, but for link 2
    $('#linkThree').attr('href', 'plan3.php'); //as above, but for link 3

    jQuery('a.gallery').colorbox({/*insert your key:value paired options specific to established users here*/}); //this references any <a> element with a class of 'gallery'
} else{
    //the cookie does not exist
    $('#linkOne').attr('href', 'test1.php'); //EDIT - this assigns an attribute to an element with the ID of 'linkOne'
    $('#linkTwo').attr('href', 'test2.php'); //as above, but for link 2
    $('#linkThree').attr('href', 'test3.php'); //as above, but for link 3

    jQuery('a.gallery').colorbox({/*insert your key:value paired options specific to new users here*/});
}

请参阅小提琴here,了解使用ID和类'

进行上述if($.cookie('v_regUsr')){ //the cookie exists $('#linkOne').attr('href', 'plan1.php'); //EDIT - this assigns an attribute to an element with the ID of 'linkOne' $('#linkTwo').attr('href', 'plan2.php'); //as above, but for link 2 $('#linkThree').attr('href', 'plan3.php'); //as above, but for link 3 jQuery('a.gallery').colorbox({/*insert your key:value paired options specific to established users here*/}); //this references any <a> element with a class of 'gallery' } else{ //the cookie does not exist $('a.gallery').attr('href', 'test.php'); //EDIT - this assigns an attribute to all elements with the class of 'gallery' jQuery('a.gallery').colorbox({/*insert your key:value paired options specific to new users here*/}); } 操作的基本工作示例