Click to See Complete Forum and Search --> : deleting cookies
Tricky123
12-30-2002, 11:37 AM
Hi,
I use the following to set a cookie:
var nextYear = new Date();
nextYear.setFullYear(nextYear.getFullYear()+1);
{
document.cookie=cookieName+"="+ Data +"; expires="+ nextYear.toGMTString() +"; path=/"
}
However, I wish to delete any cookies that this has set previously, before setting the new cookie but am not sure how.
TIA
khalidali63
12-30-2002, 12:10 PM
There may be other ways of doing it,but here is the code I have been using for ....yrs..
/*
@param String Name( cookie name )
*/
function deleteCookie(Name)
{
var expire = new Date();
expire.setTime(expire.getTime() -2 * 86400001);//set date to 2 days ago
document.cookie = Name + "=*; expires=" + expire.toGMTString();
}
Hope this points you to write direction
Khalid
Tricky123
12-31-2002, 05:11 AM
Yes that seems to work ok...
However, the way my cookies work is that each new cookie name is stored as part of a cookie file - the data is appended to the end. This means I have essentially multiple cookies in one whole cookie file. Your method will only delete a specific cookie in the whole cookie (do you follow?) whereas I need to delete entire cookie (the sum of all the parts)!!!
I have attached a cookie file to show you what I mean about the different cookie data sets in one cookie file. The line 'µµ4¬¬' is the cookieName variable.
Is there a generic deleteCookie function that will delete the whole cookie?
Charles
12-31-2002, 05:16 AM
Originally posted by Tricky123
Is there a generic deleteCookie function that will delete the whole cookie? No. Your only available method is to set it to expire yesterday. For the low down on cookies see http://developer.netscape.com/docs/manuals/js/client/jsref/index.html.
Tricky123
12-31-2002, 06:05 AM
I see,
but is there a way to define the name of the cookie in the delete function as something generic... Like you can use *.html (etc) to indicate all possible HTML files?
Tricky123
12-31-2002, 06:38 AM
Sorted!!
If you use:
var allCookies = document.cookie;
var expire = new Date();
expire.setTime(expire.getTime() -2 * 86400001); //set date to 2 days ago
document.cookie = allCookies + "=*; expires=" + expire.toGMTString() +"; path=/";
this deletes all the cookies set using document.cookie at that path.
Thanks for all the help!! :D