I want to remove a cookie I set. If I do this by giving it a expiration date sometime in the past it gets marked as "expires: at end of session". I've noticed that other sites manage to delete the cookie immediately somehow. For example when you logout here on stackoverflow the "user" cookie disappears right away. How?
+1
A:
You can't really influence how and when the browser will destroy expired cookies.
I presume here on SO after you logout, there is a redirect issued, so the browser gets an opportunity to delete the cookie right now.
Developer Art
2009-10-28 08:48:07
Ok, thanks. Is that guaranteed or does it depend on the browser's implementation?
dsimard
2009-10-28 10:07:11
That will depend on the browser implementation. The browser has only comply in that it won't send expired cookies to the server. What to do with them further, whether and when purge them, is decided by the browser writers.
Developer Art
2009-10-28 10:09:53
A:
I've been using javascript to get rid of a cookie, which has been working well (assuming that the browser has javascript turned on)
You will need to download jquery.cookie.js and include it.
<script type="text/javascript" src="jquery.cookie.js" ></script>
<script type="text/javascript">
var myCookie= readCookie('myCookie');
if (!!myCookie) {eraseCookie('myCookie');}
</script>
Of course, if required, you can allow the server to decide whether or not to emit this javascript.
brodie
2010-06-09 03:16:46