如何使用JavaScript删除所有cookie?
要使用JavaScript删除所有cookie,您可以尝试运行以下代码。在这里,我们使用数组和split()
方法来获取所有cookie并最终将其删除
示例
<!DOCTYPE html><html>
<head>
<script>
var num = 1;
function addCookie(){
document.cookie = num+" = "+num;
num++;
}
function listCookies(){
var result = document.cookie;
document.getElementById("list").innerHTML = result;
}
function removeCookies() {
var res = document.cookie;
var multiple = res.split(";");
for(var i = 0; i < multiple.length; i++) {
var key = multiple[i].split("=");
document.cookie = key[0]+" =; expires = Thu, 01 Jan 1970 00:00:00 UTC";
}
}
</script>
</head>
<body>
<button onclick = 'addCookie()'>ADD</button><br>
<button onclick = 'listCookies()'>LIST COOKIES</button><br>
<button onclick = 'removeCookies()'>REMOVE</button>
<h1>Cookies List</h1>
<p id = "list"></p>
</body>
</html>
以上是 如何使用JavaScript删除所有cookie? 的全部内容, 来源链接: utcz.com/z/334758.html