如何设置这个cookie永不过期 [英] how to set this cookie to never expire

查看:1598
本文介绍了如何设置这个cookie永不过期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个函数来创建一个传递cookie的名称,值和过期(以天为单位)的cookie。

I have a function to create a cookie passing in the name, value and expiration (in days) of the cookie.

这里是函数:

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

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);
        }
    }
}

但是我需要做什么来设置cookie永不过期?

The function works as expected, but what do I need to do to set the cookie never expire?

推荐答案

没有办法设置为永不过期
这不是javascript的限制,它不是cookie的规范的一部分 http://www.faqs .org / rfcs / rfc2965.html

There is no way to set to never expire . It's not a javascript limitation, it's just not the part of the specification of the cookie http://www.faqs.org/rfcs/rfc2965.html.

您可以设置为将来的远期日期。例如设置为20年从现在调用 setCookie 20 * 365 exdays 参数,因为你 setCookie 函数期望它到期前多少天。如下

You can set to a far date in the future. for example to set it for 20years from now call setCookie with 20*365 as exdays parameter as you setCookie function expect how many days before it expires. Like follows

setCookie('cookiename','cookie_val',20*365);

这篇关于如何设置这个cookie永不过期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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