为了保护用户信息,在他关闭浏览器的时候清除用户的登陆缓存信息
当初遇到这个问题 想了四种方法 总有一种你可以用的
放到<script></script>里边呦~
方法一:
window.onbeforeunload = onbeforeunload_handler; window.onunload = onunload_handler; function clearAllCookie() { alert(1) var keys = document.cookie.match(/[^ =;]+(?==)/g); console.log(keys,'---'); if(keys) { for(var i = keys.length; i--;) document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString() } } function onbeforeunload_handler(){ clearAllCookie() var warning="确认退出?"; return warning; }
方法二:
<script> window.onbeforeunload = onbeforeunload_handler; function clearAllCookie() { alert(1) var keys = document.cookie.match(/[^ =;]+(?==)/g); console.log(keys,'---'); if(keys) { for(var i = keys.length; i--;) document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString() } } function onbeforeunload_handler(){ clearAllCookie() var warning="确认退出?"; return warning; } </script>
方法三:
<script> function clearAllCookie() { var keys = document.cookie.match(/[^ =;]+(?==)/g); console.log(keys,'---'); if(keys) { for(var i = keys.length; i--;) document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString() } } clearAllCookie() </script>
方法四:
function onbeforeunload_handler(){ var keys = document.cookie.match(/[^ =;]+(?==)/g); console.log(keys,'---'); if(keys) { for(var i = keys.length; i--;) document.cookie = keys[i] + '=0;expires=' + new Date(0).toUTCString() } var warning="确认退出?"; return warning; }