用于在第x次页面浏览后显示内容的JS cookie

我试图找到如何设置一个JavaScript cookie,以便在访问者的第4次页面浏览后显示一个fancybox弹出窗口。

这是我用来显示Fancybox弹出窗口的代码,但正如您所看到的,这只在第一次网页浏览时显示代码,并在一天后过期

function setCookie(c_name,value,exdays) { var exdate=new Date(); exdate.setDate(exdate.getDate() + exdays); var c_value=escape(value) + ((exdays==null) ? "" : "; expires="+exdate.toUTCString()); document.cookie=c_name + "=" + c_value + ";domain=.mysite.net;path=/"; } function getCookie(c_name) { var i,x,y,ARRcookies=document.cookie.split(";"); for (i=0;i<ARRcookies.length;i++) { x=ARRcookies[i].substr(0,ARRcookies[i].indexOf("=")); y=ARRcookies[i].substr(ARRcookies[i].indexOf("=")+1); x=x.replace(/^\s+|\s+$/g,""); if (x==c_name) { return unescape(y); } } } jQuery(document).ready(function() { var show = getCookie("fancyreg"); if(show==null || show=="") { $.fancybox( { 'type' : 'iframe', 'href' : 'http://www.mysite.net/popup/', //URL to popup page or image 'autoDimensions' : false, 'width' : 480, 'height' : 260, 'transitionIn' : 'none', 'transitionOut' : 'none' } ); setCookie('fancyreg','1',1); } }); 

我还想如果你可以帮助我如何在我现有的代码中添加延迟,以便在3秒(3000毫秒)后弹出显示。

我尝试使用setTimeout(function()如下所示

   jQuery(document).ready(function() { setTimeout(function() { $.fancybox({ 'type' : 'iframe', 'href' : 'http://www.mysite.net/popup/', //URL to popup page or image 'autoDimensions' : false, 'width' : 480, 'height' : 260, 'transitionIn' : 'none', 'transitionOut' : 'none' }) }, 4000); });  

但它不起作用。

至于控制浏览量,我不知道如何设置,也没有找到任何资源来帮助我完成这个。

非常感谢

假设您正在使用jQuery Cookie插件 ,那么您可以使用以下代码在3秒后启动fancybox,即同一天的第4页访问:

 $(document).ready(function () { // create cookie var visited = $.cookie('visited'); // visited = 0 if (visited >= 3) { // open fancybox after 3 secs on 4th visit or further on the same day setTimeout(function () { $.fancybox.open({ // your fancybox API options here }); }, 3000); } else { visited++; // increase counter of visits // set new cookie value to match visits $.cookie('visited', visited, { expires: 1 // expires after one day }); return false; } }); // ready 

参见工作DEMO