【发布时间】:2013-01-23 12:11:50
【问题描述】:
我正在尝试查找如何设置 javascript cookie,以便在访问者的第 4 次网页浏览之后显示一个花式框弹出窗口。
这是我用来显示 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() 如下
<script type="text/javascript">
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);
});
</script>
但它不起作用。
至于控制浏览量,我不知道如何设置,也找不到任何资源来帮助我解决这个问题。
非常感谢
【问题讨论】:
-
嗨 Jan,我已经使用 setTimeout JS 函数添加了弹出延迟的代码。
-
您可能需要使用 jQuery cookie 插件。查看stackoverflow.com/a/8305703/1055987 了解更多信息。您可以将 cookie 设置为 4 天后过期,然后打开 fancybox
-
感谢肯尼迪。在第 3 次、第 4 次或第 x 次网页浏览后显示 fancybox 的任何提示。
-
@valdroni : 看看我的回答......如果你考虑的话,请做meta.stackexchange.com/a/5235
标签: jquery cookies fancybox fancybox-2 pageviews