Link to home
Start Free TrialLog in
Avatar of anderlecht
anderlecht

asked on

How to delete immediately a cookie ?

I try to delete my cookie by setting the expire date in the past.  That works but the cookie is deleted only when I exit the browser.  How can I delete immediately a cookie in javascript ?
Avatar of HolySpirit
HolySpirit

Here is a simple script that writes, reads and delete a cookie, i have tried on Mozilla 0.9.1 and Netscape 4.77, and the cookie is deleted immediately, i.e. no problem, you can try it in your browser and see it works or not ...

<html>
<head>
<title>test delete cookie</title>

<script type="text/javascript" language="JavaScript">
<!--

function setCookie()
{
    document.cookie = "testcookie=testing";
}

function delCookie()
{
    document.cookie = "testcookie=;expires=Fri, 02-Jan-1970 00:00:00 GMT";
}

function readCookie()
{
    alert(document.cookie);
}

//-->
</script>
</head>

<body>
<a href="javascript:setCookie()">Set Cookie</a>
<a href="javascript:delCookie()">Delete Cookie</a>
<a href="javascript:readCookie()">Read Cookie</a>
</body>
</html>
Avatar of anderlecht

ASKER

That method doesn't work with IE5.5. The cookie is deleted only if you quit the browser.
than it should be the issue of the browser ...
ASKER CERTIFIED SOLUTION
Avatar of Michel Plungjan
Michel Plungjan
Flag of Denmark image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
haha ... may be than we can check the value of the cookie and if it is empty, than treat like as it does not exist ~ haha ...
Exactly. That is the way to do it:

cook=getCookie('nameOfCookie')
if (cook) alert('nameOfCookie:'+cook)
else alert('nameOfCookie not set or empty')

Michel
haha ... thx mplungjan to clarify my concept :)
No comment has been added lately, so it's time to clean up this TA.
I will leave a recommendation in the Cleanup topic area that this question is:
points to mplungjan
Please leave any comments here within the next seven days.
 
PLEASE DO NOT ACCEPT THIS COMMENT AS AN ANSWER!
 
ahosang
EE Cleanup Volunteer
per recommendation

SpideyMod
Community Support Moderator @Experts Exchange