简体   繁体   English

如何清除此 JavaScript cookie?

[英]How do I clear this JavaScript cookie?

I am trying to clear a cookie but am having difficulty.我正在尝试清除 cookie,但遇到了困难。 Basically, all I am trying to do is change the body background colour based on the value in the cookie.基本上,我要做的就是根据 cookie 中的值更改正文背景颜色。 I can create the cookie fine, but my "clearCookie" function doesn't seem to be clearing it like I would expect it to.我可以很好地创建 cookie,但是我的“clearCookie”函数似乎并没有像我期望的那样清除它。

function setTheme () {

    if (document.cookie) {
        document.cookie += "; max-age=0";
    }

    document.cookie = $('input:radio[name=theme]:checked').val() + "; max-age=86400; path=/";
    $('body').css('background', document.cookie);
}

function clearCookie () {

    if (document.cookie) {
        document.cookie += "; max-age=0";
        $('body').css('background', '#F2F2F2');
    }

}

$(document).ready(function () {

    if (document.cookie != "") {
        $('body').css('background', document.cookie);
    }

    $('#theme').click(setTheme);
    $('#default').click(clearCookie);

});

您还应该在清除时添加路径:

document.cookie += "; max-age=0; path=/";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM