Javascript 如何使用我拥有的此代码将 cookie 设置为在 x 天后过期?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6561687/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-23 22:17:51  来源:igfitidea点击:

How can I set a cookie to expire after x days with this code I have?

javascriptcookies

提问by user815598

Possible Duplicate:
What is the “best” way to get and set a single cookie value using JavaScript

可能的重复:
使用 JavaScript 获取和设置单个 cookie 值的“最佳”方法是什么

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 的容器,所以当我按下 X 时,div 容器关闭。但是,当您刷新页面时,div 容器将再次出现。

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 容器(或者像 30 天这样的固定时间)?

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.

如果可能的话,我想更进一步,并且当这个人按下 X 按钮时,他/她将永远不会在我的网站上再次看到 div 容器,因为我计划在我的网站上实施相同的 div 容器.

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

希望这不会太令人困惑,并且有人可以帮助我。谢谢!

HTML Code:

HTML代码:

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

CSS Code:

CSS 代码:

#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 (Place at bottom of file, before tag.) Code:

JavaScript(放置在文件底部,标记之前。)代码:

<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>

回答by jfriend00

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:

要为您的 cookie 设置 30 天的过期时间,您需要为 cookie 添加一个过期时间。这是一个非常简单的 cookie 函数,它设置了 N 天的 cookie:

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=/";
}

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

通过在 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 closeAdis 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.

然后,当您的页面加载时(您必须等到页面加载完成后再尝试修改它),您可以检查您的 cookiecloseAd是否已设置,如果已设置,请执行代码以隐藏广告。为了获得更无闪烁的观看体验(因此广告不会首先显示打开,然后关闭),您可以从隐藏广告开始,仅在没有 cookie 时才显示它。或者,如果没有cookie,您只能将其动态添加到页面中。