赞
踩
只需一个js函数即可:
- //清除所有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()
- }
- }
- <!DOCTYPE html>
- <html>
-
- <head>
- <meta charset="UTF-8">
- <script src="js/jquery.min.js"></script>
- <script src="js/jquery.cookie.js"></script>
- <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] + '=0;expires=' + new Date(0).toUTCString()
- }
- }
- </script>
- <title></title>
- </head>
-
- <body>
-
- <p></p>
-
- <button οnclick="clearAllCookie();">清除所有的cookie</button>
-
- </body>
-
- </html>
当点击清除所有cookie的按钮后:
Cookie就被清除了。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。