如何设置一个cookie在x天后使用此代码已过期? [英] How can I set a cookie to expire after x days with this code I have?

查看:131
本文介绍了如何设置一个cookie在x天后使用此代码已过期?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


可能重复:

代码(我没有做),这基本上使一个容器与X,所以当我按下X,div容器关闭。但是,当您刷新页面时,div容器将重新出现。

So I have this code (which I did not make) which basically makes a container with an X, so when I press the X, the div container closes. However, when you refresh the page, the div container will reappear again.

如何做到这一点,当人按下X按钮,人永远不会看到div容器(或在一个设定的时间,如30天)?

How can I make it so when the person presses the X button, the person will never see the div container ever again (or for a set amount of time like 30 days) on that page?

我想再进一步,如果可能的话,因此当用户按下X按钮时,他/她将不会在整个网站中再次看到div容器,因为我计划在整个网站上实施同一个div容器。

I want to take it one step further, if possible, and make it so when the person presses the X button, he/she will never see the div container again throughout my site, as I plan on implementing the same div container throughout my site.

希望这不是太混乱,有人可以帮助我。谢谢!

Hope this isn't too confusing, and that someone can help me out. Thanks!

HTML代码:

<div id="bottom_ad">
    <div id="close_ad" onclick="close_bottom_ad();">X</div>
    <!-- Ad Content -->
</div>

CSS
代码:

CSS Code:

#bottom_ad
{
    position: absolute;
    right: 0;
    bottom: 0;
    left: 0;
    height: 80px;
    background: #000;
}
#close_ad
{
    position: absolute;
    top: 8px;
    right: 8px;
    width: 15px;
    height: 15px;
    color: #fff;
}

JavaScript(位于文件底部,位于标记之前)
代码:

JavaScript (Place at bottom of file, before tag.) Code:

<script type="text/javascript">
    // I may have sorta kinda taken this function from W3Schools. :S
    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);
            }
        }
    }

    var ad_cookie = getCookie("closedAd");

    if (ad_cookie != null && ad_cookie != "")
    {
        document.getElementById("bottom_ad").style.display="none";
    }

    function close_bottom_ad()
    {
        document.getElementById("bottom_ad").style.display="none";
        document.cookie = "closedAd=true;";
    }
</script>


推荐答案

要在Cookie上设置30天的过期需要向cookie添加到期时间。这里有一个非常简单的cookie函数,用于设置N天的cookie:

To set a 30 day expiration on your cookie, you need to add an expiration time to the cookie. Here's a pretty simple cookie function that sets a cookie for N days:

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

通过在cookie中设置适当的过期日期,

By setting an appropriate expiration date in the cookie, it will be removed automatically at that time.

然后,当您的网页加载时(您必须等到网页加载完毕后才尝试修改),您可以检查cookie closeAd 已设置,如果是,请执行代码以隐藏广告。为了获得更加无闪烁的观看体验(因此广告不会首先展示,然后关闭),您可以先将广告隐藏起来,如果没有Cookie,则只显示该广告。或者,如果没有Cookie,您只能动态将其添加到网页。

Then, when your page loads (you will have to wait until the page has finished loading before attempting to modify it), you can check to see if your cookie closeAd is set and if so, execute the code to hide the ad. For a more flicker free viewing experience (so the ad doesn't first show open, then close), you can either start out with the ad hidden and only show it if there is no cookie. Or you can only dynamically add it to the page if there is no cookie.

这篇关于如何设置一个cookie在x天后使用此代码已过期?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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