要删除通过document.cookie
设置的cookie,可以通过以下两种方法之一来实现:
document.cookie = "cookieName=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/";
document.cookie
的split()
方法和indexOf()
方法来查找并删除特定的cookie。例如:function deleteCookie(cookieName) {
var cookies = document.cookie.split("; ");
for (var i = 0; i < cookies.length; i++) {
var cookie = cookies[i];
var index = cookie.indexOf("=");
var name = index > -1 ? cookie.substr(0, index) : cookie;
if (name === cookieName) {
document.cookie = name + "=; expires=Thu, 01 Jan 1970 00:00:00 UTC; path=/";
}
}
}
deleteCookie("cookieName");
这两种方法都可以用来删除通过document.cookie
设置的cookie。