Home >Web Front-end >JS Tutorial >How to delete all cookies in javascript

How to delete all cookies in javascript

coldplay.xixi
coldplay.xixiOriginal
2021-04-06 16:44:365903browse

Javascript method to delete all cookies: use the [clearAllCookie()] function, the code is [function clearAllCookie() {var keys = document.cookie.match(/[^=;]]].

How to delete all cookies in javascript

The operating environment of this tutorial: windows7 system, javascript version 1.8.5, DELL G3 computer.

How to delete all cookies using javascript:

Just one js function is enough:

//清除所有cookie函数
function clearAllCookie() {
var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
if(keys) {
for(var i = keys.length; i--;)
document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString()
}
}

All the codes tested are as follows:



 




<script>
$(document).ready(function() {
//所创建的cookie有效期默认到用户关闭浏览器为止
$.cookie(&#39;the_cookie&#39;, &#39;五颜六色千变万化&#39;);
//创建一个cookie并设置 cookie的有效路径: 
$.cookie(&#39;the_cookie_expires_07&#39;, &#39;世界是座魔方大厦&#39;, {
expires: 7
});
//读取cookie
var value = $.cookie(&#39;the_cookie&#39;);
var value_07 = $.cookie(&#39;the_cookie_expires_07&#39;);
$(&#39;p&#39;).html(&#39;读取cookie的值:&#39; + value + &#39;<br />&#39; + &#39;读取cookie存在7天的值:&#39; + value_07);
});
 
//清除所有cookie函数
function clearAllCookie() {
var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
if(keys) {
for(var i = keys.length; i--;)
document.cookie = keys[i] + &amp;#39;=0;expires=&amp;#39; + new Date(0).toUTCString()
}
}
</script>


Write cookies in the browser

How to delete all cookies in javascript

After clicking the button to clear all cookies:

How to delete all cookies in javascript

##Related free learning recommendations: javascript(Video)

The above is the detailed content of How to delete all cookies in javascript. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn