jQuery 如何使用jquery删除所有cookie
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2253758/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to delete all cookies with jquery
提问by swan
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 cookie plugin? I can't seem to find examples in Klaus site and here.
我想分配一个复选框来一次性激活和清除以前存储在我的表单中的所有 cookie。我将如何使用 jquery cookie 插件做到这一点?我似乎无法在克劳斯网站和这里找到示例。
Any hint would be very much appreciated.
任何提示将不胜感激。
Thanks
谢谢
回答by Nick Craver
The accepted answer in this questionshould 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
在所有情况下都不需要插件,有时一个简单的 JavaScript 片段就可以了……jQuery 在这里真的一点帮助都没有
回答by Artem Barger
You do not need to use jquery for that, only pure javascript:
您不需要为此使用 jquery,只需要使用纯 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);
然后调用 setCookie(cookieName, null, -1);