使用 JavaScript 清除所有 cookie [英] Clearing all cookies with JavaScript
本文介绍了使用 JavaScript 清除所有 cookie的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何使用 JavaScript 删除当前域的所有 cookie?
How do you delete all the cookies for the current domain using JavaScript?
推荐答案
function deleteAllCookies() {
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var eqPos = cookie.indexOf("=");
var name = eqPos > -1 ? cookie.substr(0, eqPos) : cookie;
document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}
}
请注意,此代码有两个限制:
Note that this code has two limitations:
- 它不会删除设置了
HttpOnly
标志的 cookie,因为HttpOnly
标志会禁用 Javascript 对 cookie 的访问. - 它不会删除使用
Path
值设置的 cookie.(尽管这些 cookie 会出现在document.cookie
中,但如果不指定与设置它相同的Path
值,就无法删除它.)
- It will not delete cookies with
HttpOnly
flag set, as theHttpOnly
flag disables Javascript's access to the cookie. - It will not delete cookies that have been set with a
Path
value. (This is despite the fact that those cookies will appear indocument.cookie
, but you can't delete it without specifying the samePath
value with which it was set.)
这篇关于使用 JavaScript 清除所有 cookie的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文