javascript - How to kill zombie cookies -
i'm not sure if question related this one or not.
ie9 deletes cookie after closing browser (expected) chrome 12, firefox 5, , opera 11 not. (during testing of example below, each browser closed after clicking "delete account." reopened after short period of time , in ie9 cookies still there.)
use case: cookie expires 1 year after last visit user. account deletion should remove cookie.
question:
(1/2) why ie9 right (expected) thing , others not?
(2/2) how can ensure browsers destroy cookie?
example:
login.html
<!doctype html> <html> <head> <title>create cookie example</title> <script> function setcookie() { var expdate = new date(); expdate.setdate(expdate.getdate() + 365); document.cookie = "fakecookie=" + escape("fake value") + "; expires=" + expdate.togmtstring(); } </script> </head> <body onload="setcookie()"> <h1>welcome</h1> <p>lorem ipsum...</p> <hr size="1" /> <p><a href="profile.html">user profile</a></p> </body> </html>
profile.html
<!doctype html> <html> <head> <title>delete cookie example</title> <script> function deleteconfirm() { if ( confirm("are sure want delete account? " + "all data lost; action cannot undone!") ) deleteconfirmed() else return false return true; } function deleteconfirmed() { document.cookie = "fakecookie=; expires=thu, 01-jan-70 00:00:01 gmt"; } </script> </head> <body> <h1>user profile</h1> <p>lorem ipsum...</p> <hr size="1" /> <p><a href="index.html" onclick="return deleteconfirm()">delete account</a></p> </body> </html>
edit: original post incorrectly identified login.html index.html (forming circular reference recreate cookie when "account" deleted.)
the op came answer , edited question. repost keep solution in answer post, semantics.
<script> function deleteconfirm() { if ( confirm("are sure want delete account? " + "all data lost; action cannot undone!") ) deleteconfirmed(); // <-- ** missed semicolon here ** else return false; // <-- ** , here ** return true; } function deleteconfirmed() { document.cookie = "fakecookie=; expires=thu, 01-jan-70 00:00:01 gmt"; } </script>
Comments
Post a Comment