Forum Moderators: open

Message Too Old, No Replies

Using Javascipt to Clear Cookies

Is this possible?

         

texignweb

6:24 am on Jun 15, 2005 (gmt 0)

10+ Year Member



Is there a script anyone knows of that can clear cookies. I would like to put a link on my site that reads "Click here to clear your cookies".

RonPK

7:54 am on Jun 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



You can clear a cookie if you know the name and if the script is on the same domain as the cookie. Simply set the value to empty and the expiration date to somewhere in the past:

var mydate = new Date();
mydate.setTime(mydate.getTime() - 1);
document.cookie = "username=; expires=" + mydate.toGMTString();

rocknbil

4:29 pm on Jun 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



RonPK, question for you, I've set cookies using server-side languages and have never been able to alter them at all using Javascript. I always thought (probably errantly) this was because Javascript cookies are document specific, hence document.cookie. Is there just Something Else I'm Missing?

My workaround has been that if I will be altering the cookie with Javascript, I make sure I set it in Javascript.

RonPK

10:30 pm on Jun 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Can't help you there, rocknbil. Just a wild guess: maybe it has to do with the path parameter? I.e., your cookie has been set with path=/ and your javascript is in /somedir?

rocknbil

11:15 pm on Jun 15, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Well if I set a persistent cookie I always follow the cookie spec, no matter how it's set. I've played around with it quite a bit always just assumed they were two different types or something and worked around it.

Now I have to add something new to revisit. Rats. :-)

texignweb

1:33 am on Jun 16, 2005 (gmt 0)

10+ Year Member



I'll explain my situation.

On my site I'll need a link/button that can do one of two things. 1- Clear all cookies or 2- Clear all cookies under a domain that ISNT mine. Number 1 would sound more realistic but I think you alluded to that it can't work.

If any of this really is possible here is the cookie info-

Site- www.paradiseaffiliates.com
Cookie Name- btag

Anything that could clear that cookie when someone just clicks a simple button on my site would be GREAT. I'm worried some people can't follow simple 'delete cookies' instructions.

Please keep in mind I know ZERO about JavaScript.

RonPK

6:16 am on Jun 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



It's a security issue: you simply cannot read, modify or delete cookies set to a different domain. As a webmaster, you wouldn't be happy if other webmasters had access to the cookies you set. As a user, I wouldn't be happy if some site would delete WebmasterWorld cookies, forcing me to log in all the time, or if some site got access to other login cookies.

rocknbil

4:04 pm on Jun 16, 2005 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



tex, are you concerned that other-site cookies are affecting your project? For the reasons RPK mentioned, I assure you they are not.

The only way you could clear ALL of someone's persistent cookies is to write an ActiveX object that access the local cookie files/entries. Bad idea.