Forum Moderators: open
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
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.
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
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.