2014-09-05 79 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>