We help IT Professionals succeed at work.

How to delete immediately a cookie ?

anderlecht
anderlecht asked
on
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 ?
Comment
Watch Question

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>

Author

Commented:
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 ...
IT Expert
CERTIFIED EXPERT
Top Expert 2009
Commented:
You cannot DELETE the cookie.
You can MARK it fro deletion with a date in the past.

YOu can set a NEW EMPTY cookie IMMEDIATELY

function getCookie(Name) {
   var search = Name + "="
   if (document.cookie.length > 0) {
      offset = document.cookie.indexOf(search)
      if (offset != -1) {
         offset += search.length
         end = document.cookie.indexOf(";", offset)
         if (end == -1) end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))
      }
   }
}

function setCookie (name,value,expires,path,theDomain,secure) {
   var theCookie = name + "=" + escape (value) +
   ((expires)    ? "; expires=" + expires.toGMTString() : "") +
   ((path)       ? "; path="    + path   : "") +
   ((theDomain)  ? "; domain="  + theDomain : "") +
   ((secure)     ? "; secure"            : "");
   document.cookie = theCookie;
}

function delCookie(name,path,domain) {
   if (getCookie(name)) document.cookie = name + "=" +
      ((path)   ? ";path="   + path   : "") +
      ((domain) ? ";domain=" + domain : "") +
      ";expires=Thu, 01-Jan-70 00:00:01 GMT";
//   alert(name+' marked for deletion');
}


setCookie('nameOfCookie','value of cookie') // sets

setCookie('nameOfCookie','') // empties it
delCookie('nameOfCookie') /* marks it for deletion (unnecessary if expiry date in the future is not used */

Michel
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 ...
Michel PlungjanIT Expert
CERTIFIED EXPERT
Top Expert 2009

Commented:
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 :)
ahosangFinance Systems Developer

Commented:
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