2014-09-05 39 views
0

我已被要求扩展给定的代码,使得生成的框仅在每次访问时显示一次。 看完之后,问题似乎并不复杂,但由于某些原因,代码无法正常工作 - 现在该层完全不显示。 由于我对jQuery不太熟悉,而原作者也不可用,所以我希望有人可能会推动我向正确的方向发展? 出于测试目的,我试图将cookie设置为在2小时内过期。仅在起始页面显示jQuery Popup一次且仅在起始页面

<script type="text/javascript"> 
$.noConflict(); 
/* Popup on starting page */ 
<?php 
    if($_SERVER["REQUEST_URI"] == "/" || $_SERVER["REQUEST_URI"] == "/index.php") : 
?> 
jQuery(document).ready(function() { 
    if ($.cookie('test') !='1'){ 

jQuery("body").append("<div class=\"vbox-shadow\"></div>"); 
jQuery(".vbox-shadow").fadeIn(function() { 
jQuery("body").append("<div class=\"vbox-layer\"></div>"); 
jQuery(".vbox-layer").html("<span class=\"vbox-close\">X</span><a href=\"/link/target.html\"><img src=\"/images/popup_image.jpg\" /><br /></a>"); 
jQuery(".vbox-close").click(function(e) { 
jQuery(".vbox-layer").hide(function()  { 
jQuery(".vbox-layer").remove(); 
jQuery(".vbox-close").remove(); 
jQuery(".vbox-shadow").fadeOut(function() { jQuery(".vbox-shadow").remove(); }); 
}); 
}); 
jQuery(".vbox-shadow").click(function(e) { 
jQuery(".vbox-layer").hide(function()  { 
jQuery(".vbox-layer").remove(); 
jQuery(".vbox-close").remove(); 
jQuery(".vbox-shadow").fadeOut(function() { jQuery(".vbox-shadow").remove(); }); 
}); 
}); 
$.cookie('test', '1', { expires:7200000}); 
});} 
}); 
<?php 
endif; 
?> 
</script> 

回答

0

Cookie也可能已过期。所以在这种情况下$ .cookie('test')将返回undefined,尝试将您的$ .cookie('test')!='1'更改为if(!$。cookie('test')){

+0

感谢您的建议,@ artm! 尽管似乎没有改变任何东西 - 只要我添加行来检查/设置cookie,图层根本不显示。 另外,网站管理员工具包不会显示“测试”-cookie曾经被设置在首位。 – LoneWolf 2014-09-05 09:20:12

+0

有些东西看起来不对,jQuery.Cookie的documentation指出到期日期是DAYS中的一个数字。 为了理智,您可能希望将cookie的路径设置为根文件夹,如下所示: '$ .cookie('test','1',{path:'/',expires:7200000} );' – DevlshOne 2014-09-05 09:20:13

+0

在jQuery(document).ready(function(){)中放置了一个断点,并检查$ .cookie('test')的返回结果为 – artm 2014-09-05 09:21:52

0

使用PHP生成的cookie来管理它,如下所示:

<script type="text/javascript"> 
$.noConflict(); 
/* Popup on starting page */ 
<?php 
if(($_SERVER["REQUEST_URI"] == "/" || $_SERVER["REQUEST_URI"] == "/index.php")&&($_COOKIE['NoPopup']!='true')) : 
?> 
jQuery(document).ready(function() { 

jQuery("body").append("<div class=\"vbox-shadow\"></div>"); 
jQuery(".vbox-shadow").fadeIn(function() { 
jQuery("body").append("<div class=\"vbox-layer\"></div>"); 
jQuery(".vbox-layer").html("<span class=\"vbox-close\">X</span><a href=\"/link/to/page.html\"><img src=\"/images/popup.jpg\" /><br /></a>"); 
jQuery(".vbox-close").click(function(e) { 
jQuery(".vbox-layer").hide(function()  { 
jQuery(".vbox-layer").remove(); 
jQuery(".vbox-close").remove(); 
jQuery(".vbox-shadow").fadeOut(function() { jQuery(".vbox-shadow").remove(); }); 
}); 
}); 
jQuery(".vbox-shadow").click(function(e) { 
jQuery(".vbox-layer").hide(function()  { 
jQuery(".vbox-layer").remove(); 
jQuery(".vbox-close").remove(); 
jQuery(".vbox-shadow").fadeOut(function() { jQuery(".vbox-shadow").remove(); }); 
}); 
}); 
}); 
}); 

<?php 
endif; 
$value='true'; 
setcookie("NoPopup", $value, time()+60*60*6); 
?> 
</script>