Cookie每30天显示一个弹出窗口 [英] Cookie to show a popup every 30 days

查看:134
本文介绍了Cookie每30天显示一个弹出窗口的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

<script type="text/javascript">
jQuery(document).ready(function(){
if (document.cookie.indexOf('visited=false') == -1)
{
var fifteenDays = 1000*60*60*24*30;
var expires = new Date((new Date()).valueOf() + fifteenDays);
document.cookie = "visited=true;expires=" + expires.toUTCString();
$.colorbox({width:"400px", inline:true, href:"#exestylepopups"});
}
});
</script>

我的上述代码有什么问题我的facebook喜欢popup是在每次页面加载到我的博客。

What's wrong with my above code my facebook like popup is called every time the page is loaded in my blog. I just to show only one time every 30 days.How i can do that?

推荐答案

你的特定实现看起来像是你检查'visited = false'的cookie,但将cookie设置为'visited = true' c $ c>如果语句永远不匹配。

Your particular implementation looks like it has you checking the cookie for 'visited=false', but setting the cookie to 'visited=true' so your if statement will never match.

这里是我使用的cookie函数,当我的环境还没有它们时:

Here are the cookie functions I use when my environment doesn't already have them:

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
        var c = ca[i];
        while (c.charAt(0)==' ') c = c.substring(1,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

一旦你有了这些,你可以这样做:

Once you have those, you can do this:

jQuery(document).ready(function() {
    var visited = readCookie('visited');
    if (!visited || visited !== "true") {
        createCookie('visited', "true", 30);
        $.colorbox({width:"400px", inline:true, href:"#exestylepopups"});
    }
});

这篇关于Cookie每30天显示一个弹出窗口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆