0

我有这个代码来设置cookie:

function SetCookie(cookieName, cookieValue, nDays) {
    var today = new Date();
    var expire = new Date();
    if (nDays == null || nDays == 0) nDays = 1;
    expire.setTime(today.getTime() + 3600000 * 24 * nDays);
    document.cookie = cookieName + "=" + escape(cookieValue) + ";expires=" + expire.toGMTString(), ';path = /';
}

要设置 cookie,我必须使用所有参数调用函数。

SetCookie("number", "1", 10);

但是当我使用 ReadCookie 函数获取值时,cookie 值不是 1,即:

function ReadCookie(cookieName) {
    var theCookie = "" + document.cookie;
    var ind = theCookie.indexOf(cookieName + "=");
    if (ind == -1 || cookieName == "") return "";
    var ind1 = theCookie.indexOf(";", ind);
    if (ind1 == -1) ind1 = theCookie.length;
    return unescape(theCookie.substring(ind + cookieName.length + 1, ind1));
}

输出而不是“1”是:

e85751a63874d221ff23323575137233
4

1 回答 1

0

您的代码似乎工作正常,但我想我会发布一些我编写的通过 JavaScript 处理 cookie 的代码,让您对可以做什么有更多的想法。我基于此处的 MDN 示例,但具有我想要的功能(对象查找、缓存直到更改)。随意为您的项目提取代码片段(或全部使用)。

cookie.setItem('number', 1, 10 * 24 * 60 * 60); // expires is Number => seconds
cookie.getItem('number'); // "1"

函数 cookie使字符串正确格式化以供使用document.cookie,它需要参数key、val、expires、path、domain、secure。参数expires可以是Number(从现在开始的秒数或Infinity)、DateString

函数 cookie.setItem具有相同的参数,cookie但也应用它。如果只给出一个,则假定它是一个完整的 cookie 格式的String

函数 cookie.getItem只需要一个参数key

函数 cookie.removeItem有参数key, path。你只需要使用key

var cookie = (function () {
    var cookieString = '',
        items = {},
        expireNever = new Date(new Date().valueOf() + 64e10).toGMTString(), // 20 years
        expireNow = 'Thu, 01 Jan 1970 00:00:00 GMT',
        cookie,
        __update,
        supportsMaxAge = 1;
    __update = function () {
        var a, i, j, o = {};
        cookieString = document.cookie;
        a = cookieString.split('; ');
        i = a.length;
        while (i--) {
            j = a[i].indexOf('=');
            o[decodeURIComponent(a[i].slice(0, j))]
                = decodeURIComponent(a[i].slice(j + 1));
        }
        items = o;
    }
    cookie = function cookie(key, val, expires, path, domain, secure) {
        var s = encodeURIComponent(''+key) + '='
              + encodeURIComponent(''+val);
        if (!key) return '';
        if (expires || expires === 0) {
            switch (expires.constructor) {
                case Number:
                    if (expires === Infinity) expires = 'expires=' + expireNever;
                    else {
                        if (supportsMaxAge) expires = 'max-age=' + expires;
                        else expires = 'expires=' + new Date(new Date().valueOf() + expires * 1000).toGMTString();
                    }
                    break;
                case Date:
                    expires = 'expires=' + expires.toGMTString();
                    break;
                default:
                case String:
                    expires = 'expires=' + expires;
                    break;
            }
            s = s + '; ' + expires;
        }
        if (domain) s = s + '; domain=' + domain;
        if (path) s = s + '; path=' + path;
        if (secure) s = s + '; secure';
        return s;
    };
    cookie.getNames = function () {
        if (document.cookie !== cookieString) __update();
        return Object.keys(items);
    };
    cookie.getItem = function (key) {
        var s;
        if (document.cookie !== cookieString) __update();
        s = items[key];
        if (s || s === '') return s;
        return null;
    };
    cookie.setItem = function (key, val, expires, path, domain, secure) {
        var s;
        if (!key) return null;
        if (val === void 0) s = key;
        else s = cookie(key, val, expires, path, domain, secure);
        document.cookie = s;
        return s;
    };
    cookie.hasItem = function (key) {
        if (cookie.getItem(key) === null) return false;
        return true;
    };
    cookie.removeItem = function (key, path) {
        if (cookie.setItem(key, '', expireNow, path)) return true;
        return false;
    };
    // find out if max-age is supported
    cookie.setItem('test_cookie_max_age', '1', 0);
    if (cookie.getItem('test_cookie_max_age')) {
        supportsMaxAge = 0;
        cookie.removeItem('test_cookie_max_age');
    }
    __update();
    return cookie;
}());
于 2013-05-05T18:26:26.603 回答