用JavaScript清除所有cookie如何使用JavaScript删除当前域的所有cookie?
3 回答
慕斯王
TA贡献1864条经验 获得超2个赞
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
)设置它的值。)
潇湘沐
TA贡献1816条经验 获得超6个赞
一衬垫
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
TA贡献1794条经验 获得超8个赞
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 + ';'; }}
添加回答
举报
0/150
提交
取消