如何使用jQuery设置和取消设置cookie,例如创建一个名为test的cookie并将值设置为1?


当前回答

Try(这里的doc, SO代码段不工作,所以运行这个)

document.cookie = "test=1"             // set
document.cookie = "test=1;max-age=0"   // unset

其他回答

2019年4月更新

读取/操作cookie不需要jQuery,所以不要使用下面的原始答案。

请访问https://github.com/js-cookie/js-cookie,使用那里不依赖jQuery的库。

基本的例子:

// Set a cookie
Cookies.set('name', 'value');

// Read the cookie
Cookies.get('name') => // => 'value'

详见github上的文档。


2019年4月前(老)

查看插件:

https://github.com/carhartl/jquery-cookie

你可以这样做:

$.cookie("test", 1);

删除:

$.removeCookie("test");

此外,要在cookie上设置一个特定天数的超时(这里是10天):

$.cookie("test", 1, { expires : 10 });

如果省略expires选项,则cookie将成为会话cookie,并在浏览器退出时删除。

要涵盖所有选项:

$.cookie("test", 1, {
   expires : 10,           // Expires in 10 days

   path    : '/',          // The value of the path attribute of the cookie
                           // (Default: path of page that created the cookie).

   domain  : 'jquery.com', // The value of the domain attribute of the cookie
                           // (Default: domain of page that created the cookie).

   secure  : true          // If set to true the secure attribute of the cookie
                           // will be set and the cookie transmission will
                           // require a secure protocol (defaults to false).
});

读取cookie的值。

var cookieValue = $.cookie("test");

更新(2015年4月):

正如下面的评论所述,开发原始插件的团队已经在一个新项目(https://github.com/js-cookie/js-cookie)中删除了jQuery依赖项,该项目具有与jQuery版本相同的功能和通用语法。显然,原来的插件并没有去任何地方。

Try(这里的doc, SO代码段不工作,所以运行这个)

document.cookie = "test=1"             // set
document.cookie = "test=1;max-age=0"   // unset

你可以在这里使用一个插件。

https://plugins.jquery.com/cookie/

然后写一个饼干做 美元。饼干(“测试”,1);

访问设置cookie做 美元.cookie(“测试”);

<script type="text/javascript">
    function setCookie(key, value, expiry) {
        var expires = new Date();
        expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));
        document.cookie = key + '=' + value + ';expires=' + expires.toUTCString();
    }

    function getCookie(key) {
        var keyValue = document.cookie.match('(^|;) ?' + key + '=([^;]*)(;|$)');
        return keyValue ? keyValue[2] : null;
    }

    function eraseCookie(key) {
        var keyValue = getCookie(key);
        setCookie(key, keyValue, '-1');
    }

</script>

你可以把饼干设置成

setCookie('test','1','1'); //(key,value,expiry in days)

你可以拿饼干

getCookie('test');

最后你可以像这样擦掉这些饼干

eraseCookie('test');

希望能对别人有所帮助:)

编辑:

如果要将cookie设置为所有路径/页/目录,则将path属性设置为cookie

function setCookie(key, value, expiry) {
        var expires = new Date();
        expires.setTime(expires.getTime() + (expiry * 24 * 60 * 60 * 1000));
        document.cookie = key + '=' + value + ';path=/' + ';expires=' + expires.toUTCString();
}

谢谢, 维姬

不需要特别使用jQuery来操作cookie。

来自QuirksMode(包括转义字符)

function createCookie(name, value, days) {
    var expires;

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

function readCookie(name) {
    var nameEQ = encodeURIComponent(name) + "=";
    var ca = document.cookie.split(';');
    for (var i = 0; i < ca.length; i++) {
        var c = ca[i];
        while (c.charAt(0) === ' ')
            c = c.substring(1, c.length);
        if (c.indexOf(nameEQ) === 0)
            return decodeURIComponent(c.substring(nameEQ.length, c.length));
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name, "", -1);
}

看一看

我如何删除一个现有的类名,并添加一个新的与jQuery和cookies?