views:

5033

answers:

4

I have written code to save the cookies in Javascript. Now I need to clear the cookie irrespective of values that I assigned.

Are there any script modules to delete all cookies that were generated by javascript?

My Sample Code:

 document.cookie = 'ppkcookie2=another test; expires=Fri, 3 Aug 2001 20:47:11 UTC; path=/'

 function createCookie(name,value,days) {
    if (days) {
     var date = new Date();
     date.setTime(date.getTime()+(days*24*60*60*1000));
     var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    var ca = document.cookie.split(';');
    for(var i=0;i < ca.length;i++) {
     var c = ca[i];
     while (c.charAt(0)==' ') c = c.substring(1,c.length);
     if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

How else could I clear all of the cookies?

Will there will be any problems when I test the code on the webserver?

+3  A: 

But on the face of it it looks OK - if you call your eraseCookie() on each of the cookies that you read from document.cookie, then all the cookies would be gone.

Maybe something like this:

var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
  eraseCookies(cookies[i].split("=")[0]);

All this with caveat:

  • You can only remove cookies created by JavaScript - if a cookie was create by the server, then you cannot remove it through JavaScript.
Guss
"You can only remove cookies created by JavaScript - if a cookie was create by the server, then you cannot remove it through JavaScript." Actually, you should be able to erase any cookies you can see.
R. Bemrose
I can't find the exact reference right now, but I've had some problems in client manipulations of cookies created by a server. I do believe that there are some issues with that at least on Firefox.
Guss
It's the "HttpOnly" flag that is catching you up. You CAN delete server cookies from javascript unless they are protected with the HttpOnly flag
Eli
Thanks for the explanation. The name is not really intuitive, I think.
Guss
A: 

If you want to delete all the cookies, why not simply do

document.cookie = "";
levik
document.cookie is not a regular string. Setting document.cookie = '' doesn't actually modify the browser cookie store - you need to specify the name, value etc of the cookie key-value-pair you want to modify/erase.
Marcus Westin
I tried this on FF, but it turns out that a new empty string(length = 0) name-value pair cookie is created.
Jichao
A: 

There is no 100% solution to delete browser cookies.

The problem is that cookies are uniquely identified by not just by their key "name" but also their "domain" and "path".

Without knowing the "domain" and "path" of a cookie, you cannot reliably delete it. This information is not available through JavaScript's document.cookie. It's not available through the HTTP Cookie header either!

However, if you know the name, path and domain of a cookie, then you can clear it by setting an empty cookie with an expiry date in the past, for example:

function clearCookie(name, domain, path){
    var domain = domain || document.domain;
    var path = path || "/";
    document.cookie = name + "=; expires=" + +new Date + "; domain=" + domain + "; path=" + path;
};
jb
A: 

Reads, writes and deletes current Web page's cookies

http://java.pakcarid.com/Cpp.aspx?sub=385&amp;ff=3043&amp;topid=40&amp;sls=25

lois