开发者

How to delete all cookies with jquery [duplicate]

开发者 https://www.devze.com 2022-12-20 04:31 出处:网络
This question already has answers here: Closed 12 years ago. Possible Duplicate: Clearing all cookies with javascript
This question already has answers here: Closed 12 years ago.

Possible Duplicate:

Clearing all cookies with javascript

I would like to have a checkbox assigned to activate and wipe out all cookies previously stored in my forms in one go. How would I do that with jquery开发者_JAVA百科 cookie plugin? I can't seem to find examples in Klaus site and here.

Any hint would be very much appreciated.

Thanks


The accepted answer in this question should accomplish what you're after:

var cookies = document.cookie.split(";");
for(var i=0; i < cookies.length; i++) {
    var equals = cookies[i].indexOf("=");
    var name = equals > -1 ? cookies[i].substr(0, equals) : cookies[i];
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}

(code is expanded for clarity from the linked answer, no need to reinvent the wheel here)

There's no need for a plugin in all cases, sometimes a simple JavaScript snippet will do...jQuery really doesn't help at all here


You do not need to use jquery for that, only pure javascript:

function setCookie(name, value, seconds) {

    if (typeof(seconds) != 'undefined') {
        var date = new Date();
        date.setTime(date.getTime() + (seconds*1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else {
        var expires = "";
    }

    document.cookie = name+"="+value+expires+"; path=/";
}

And call with setCookie( cookieName, null, -1);

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号