-
慕斯王
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";
}}请注意,此代码有两个限制:它不会用HttpOnly标志集,如HttpOnly标志禁用Javascript对cookie的访问。它将不会删除已设置为Path价值。(尽管这些cookie将出现在document.cookie,但如果不指定相同的Path)设置它的值。)
-
潇湘沐
一衬垫如果你想尽快把它粘贴进去.document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;expires=" + new Date().
toUTCString() + ";path=/"); });以及书签的代码:javascript:(function(){document.cookie.split(";").forEach(function(c) { document.cookie = c.replace(/^ +/, "").replace(/=.*/, "=;
expires=" + new Date().toUTCString() + ";path=/"); }); })();
-
幕布斯7119047
在经历了一些挫折之后,我自己完成了这个函数,它将尝试从所有路径中删除一个命名的cookie。只需为每个cookie调用它,您就应该更接近于删除每个cookie,而不是以前的每个cookie。function eraseCookieFromAllPaths(name) {
// This function will attempt to remove a cookie from all paths.
var pathBits = location.pathname.split('/');
var pathCurrent = ' path=';
// do a simple pathless delete first.
document.cookie = name + '=; expires=Thu, 01-Jan-1970 00:00:01 GMT;';
for (var i = 0; i < pathBits.length; i++) {
pathCurrent += ((pathCurrent.substr(-1) != '/') ? '/' : '') + pathBits[i];
document.cookie = name + '=; expires=Thu, 01-Jan-1970 00:00:01 GMT;' + pathCurrent + ';';
}}与以往一样,不同的浏览器有不同的行为,但这对我来说很有效。好好享受吧。