12

I'm using javascript to remove a cookie but for some reason it isn't working with Chrome. The script I'm using is;

function clearCookie() { document.cookie = 'myCookie=; expires=Thu, 01-Jan-70 00:00:01 GMT; path=/myPath/'; } 

This works on;

  • IE 8.0.6
  • Firefox 3.6.12

..but doesn't work on Chrome 7.0.517.44, after the cookie is supposed to be cleared I can still see it and the value hasn't changed.

Any ideas? Are there any user settings in Chrome that might prevent my cookie from being removed?

4
  • 1
    I don't know about chrome, so this is just a guess. Maybe chrome caches the view of cookies? Or: What happens after you quit chrome and reopen it? Is the cookie still there? Commented Nov 11, 2010 at 12:01
  • The cookie expires when the session ends, so yes, closing the browser and reopening clears it. Commented Nov 11, 2010 at 12:22
  • Also try to navigate to a different page on the same domain after you clear the cookie - does the cookie still exist? Commented Nov 11, 2010 at 13:20
  • Yeah, it does, that's the problem :( Commented Nov 11, 2010 at 13:55

4 Answers 4

2

Chrome doesn't support cookies on file:// and localhost uris. See this so question - Why does Chrome ignore local jQuery cookies?

Sign up to request clarification or add additional context in comments.

Comments

1

You need to use the right datetime format for it to work. The following should do the trick

function clearCookie() { document.cookie = 'myCookie=; expires='+new Date(0).toUTCString() +'; path=/myPath/'; } 

And of course you need to specify the exact same path and&or domain specified on cookie creation.

4 Comments

It works exactly like it should as you can see in this fiddle: jsfiddle.net/cWshc/2 - if you can't make it work you are probably not setting the right path or domain or similar.
same issue for me. Chrome is not deleting cookies when i run the clearCookie function provided.
For me this works if I use .toGMTString() (I've not tried toUTCString())
To quote MDN: toGMTString is deprecated and should no longer be used, it's only there for backwards compatibility, use toUTCString instead. (they will give you the same output)
1

You can clear a cookie in chrome, but you need to set the domain as well when creating the blank cookie to replace the current one.

Comments

0

Chrome and FF have severity problems with this. Here you can see both browsers bugs and their status is WONTFIX...

Chrome: https://code.google.com/p/chromium/issues/detail?id=128513

FF: https://bugzilla.mozilla.org/show_bug.cgi?id=443354

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.