Forum Moderators: open

Message Too Old, No Replies

delete cookies

how can I delete cookies using JavaScript

         

luckydude

3:46 pm on Mar 6, 2004 (gmt 0)

10+ Year Member



How can I delete cookies right away when user clicks on something on the page? Say, if there is something on the page with a link and if the user clicks on the link, all the cookies generated by the webpage will be deleted automatically. I know how to do it using the expiration date, but I don't want to put any expiration date.

the following code kills the cookie when it expires, but I want something that kills the cookie right after the user clicks on something. Can someone help me here please?


function KillCookie(cookieName,cookieValue)
{
//create an instance of the Date object
var expDate = new Date();
//set the date value to the past
expDate.setTime(expDate.getTime() - 1000 * 60 * 60 * 24 * 365);
//convert date to a GMT string
expDate = expDate.toGMTString();
var cookieString = cookieName + "=" + cookieValue + ";expires=" + expDate;
window.document.cookie = cookieString;
}

Your help is greatly appreciated.
Luckydude

isitreal

3:51 pm on Mar 6, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



This is the delete cookie code I use, it's a bit cleaner, doesn't really matter though, your's will work too:

function Delete_Cookie(name, path, domain) {
if (Get_Cookie(name)) document.cookie = name + "=" +
( (path)? ";path=" + path : "") +
( (domain)? ";domain=" + domain : "") +
";expires=Thu, 01-Jan-1970 00:00:01 GMT";
}

then when the user clicks on something, have this code:

<a href="somepage.htm" onclick="Delete_Cookie('cookiename', '', '');">some link</a>

You only have to worry about the domain or path variables if the cookie was set in a non default way.

luckydude

8:24 pm on Mar 7, 2004 (gmt 0)

10+ Year Member



>>You only have to worry about the domain or path variables if the cookie was set in a non default way.>>

Could you explain more about it?

>>function Delete_Cookie(name, path, domain)>>

Am I supposed to define the name, path, and domain of the cookie? I just started learning about cookies, so I don't know much. I really appreciated your help.

Luckydude

isitreal

9:45 pm on Mar 7, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member Top Contributors Of The Month



You only have to describe the domain or path if the cookie was set in a folder below the current folder.

In other words, if you set the cookie in /main/somepage.html and then try to access it from /index.html you can't unless you explicitly set the path variable on set_cookie() to be '/' , or the domain root.

Don't worry about the domain thing for now, you won't need it for a long time, if ever, it's for more advanced applications.

The best way to learn is to just play around with the stuff, try setting and deleting cookies from various parts of your website, just add a link that deletes the cookies you make, for testing purposes, then once you feel you understand how they work, get rid of those links.

luckydude

12:51 am on Mar 8, 2004 (gmt 0)

10+ Year Member



thanks isitreal, I really appreciate your help.

luckydude