View Full Version : Deleting/Erasing a Cookie

02-09-2007, 11:56 PM
:confused: Okay, I'm beside myself on this one. I have checked all over the web and I can't seem to find any info on deleting/erasing cookies. The most I have found is instructions for setting the cookie expiration date to a past date.

This works for removing the cookie value but the cookie name remains until the browser is restarted. Is there a way to completely delete/erase a cookie as a function?

I have a page that records [in a cookie] a visitor's choice of a skin for a table. They also have the option of resetting the skin to the default [by deleting/erasing the cookie] and checking the cookie to verify it is present/gone. This is why I need it to go away completely when the visitor kills it.

Any thoughts on this would be greatly appreciated.

02-10-2007, 12:40 AM
the only method I know is the past time method which you have already meantioned, why not just set the cookie to the default value?

02-10-2007, 12:51 AM

02-10-2007, 01:01 AM
To neomaximus2k:
I may have to just set the cookie to the default and be done with it.

My intention was to be forthright with visitors and allow them to see the cookie (selectedSkin="whatever_skin.gif") and verify that the cookie had been erased when they reset the skin.

This is proving much more difficult though. I am still open if anyone has further insights as to how browsers handle cookies.

To FJbrian:
Thanks for the link but I already went through that documentation. It didn't work correctly in IE 6 [imagine that]. I'll check it again though. Maybe I did something wrong.

02-10-2007, 01:05 AM
Well I went into one of my old scripts that used javascript for cookies and this is how i used to remove the cookies

function eraseCookie() {

change cookiename for the name of your cookie and try that

02-10-2007, 02:16 AM
Still no luck but I think I am narrowing the problem down. My IE browsers seem to be having problems with the cookie [retention, deleting, etc.] but it appears that my Mozilla browsers are doing better.

I need to do some more debugging and error checking throughout a series of browsers and versions.

Stay tuned. I'll post back when I have something.

Thanks for all the help.

02-10-2007, 07:49 PM
Setting the expire date prior to the current date works for me to delete the cookie immediately in IE7 and FF2 in my test. What is your cookie code?


02-12-2007, 08:44 PM
I am coding to include older browsers also and that is where some of the problem was. Also, as I mentioned, There were inconsistencies between browser engines. Basically it depended on what browser I was using [thus the need to cross-test scripts].

I started combining different scripts and found most worked on one browser engine or another. After a lot of testing, combining and refining here's a stable cross-browser answer with a delete verification [confirm]:

function skinReset(){
if (confirm("You are about to reset your skin selection to the default. Do you want to continue?")) {
alert("The skin has been reset and the cookie removed. Some browsers hold the remenants of the cookie until the browser is restarted.");
}else {
return null;

function delete_cookie (cookie_name){
var cookie_date = new Date ( ); // current date & time
cookie_date.setTime( cookie_date.getTime() - 1 );
document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString(); // IE delete cookie
document.cookie = cookie_name + '; expires=Sun, 31-Dec-2000 24:00:00 GMT; path=/'; // FF/Moz delete cookie

:thumbsup: Hopes this helps others facing similar probs.

EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum