要刪除通過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。