如何保存cookie信息?

时间:2014-10-25 09:33:45

标签: javascript date cookies save page-refresh

我认为问题不仅仅是exp.date而是启动,每次刷新页面时cookie都会重新出现。为什么不赢取饼干?

cookieScriptLoadJavaScript = function (d, b) {
    var c = document.getElementsByTagName("head")[0],
        a = document.createElement("script");
    a.type = "text/javascript", a.src = d, b != undefined && (a.onload = a.onreadystatechange =     function () 
    {
        (!a.readyState || /loaded|complete/.test(a.readyState)) && (a.onload =      a.onreadystatechange = null, c && a.parentNode && c.removeChild(a), a = undefined, b())
    }), c.insertBefore(a, c.firstChild)
}, 

InjectCookieScript = function () {


cookieScriptCreateCookie = function (n, t, i) {
        var u = "",
            r, f;
        i && (r = new Date, r.setTime(r.getTime()), u = "; expires=" + r.toGMTString()), f = "", cookieScriptDomain != "" && (f = "; domain=" + cookieScriptDomain), document.cookie = n + "=" + t + u + f + "; path=/"
    }, 

1 个答案:

答案 0 :(得分:1)

您显然需要更改 r 。如果您想永久设置cookie 2天,您可能想要使用它:

r.setDate(r.getDate() + 2);

但我建议您将其作为变量传递给您的函数。这样你就可以保持代码的动态。您的代码可能如下所示:

cookieScriptCreateCookie = function (n, t, i, q) {
        var u = "",
            r, f;
        i && (r = new Date, r.setDate(r.getDate() + q), u = "; expires=" + r.toGMTString()), f = "", cookieScriptDomain != "" && (f = "; domain=" + cookieScriptDomain), document.cookie = n + "=" + t + u + f + "; path=/"
    },