Home >Web Front-end >JS Tutorial >How to delete all cookies in javascript
Javascript method to delete all cookies: use the [clearAllCookie()] function, the code is [function clearAllCookie() {var keys = document.cookie.match(/[^=;]]].
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('the_cookie', '五颜六色千变万化'); //创建一个cookie并设置 cookie的有效路径: $.cookie('the_cookie_expires_07', '世界是座魔方大厦', { expires: 7 }); //读取cookie var value = $.cookie('the_cookie'); var value_07 = $.cookie('the_cookie_expires_07'); $('p').html('读取cookie的值:' + value + '<br />' + '读取cookie存在7天的值:' + value_07); }); //清除所有cookie函数 function clearAllCookie() { var keys = document.cookie.match(/[^ =;]+(?=\=)/g); if(keys) { for(var i = keys.length; i--;) document.cookie = keys[i] + &#39;=0;expires=&#39; + new Date(0).toUTCString() } } </script>
Write cookies in the browser
After clicking the button to clear all cookies:
##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!