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?

Qwerky
  • 18,217
  • 6
  • 44
  • 80
  • 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? – Fidi Nov 11 '10 at 12:01
  • The cookie expires when the session ends, so yes, closing the browser and reopening clears it. – Qwerky Nov 11 '10 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? – Dror Nov 11 '10 at 13:20
  • Yeah, it does, that's the problem :( – Qwerky Nov 11 '10 at 13:55

4 Answers4

2

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

Community
  • 1
  • 1
scaryman
  • 1,880
  • 1
  • 19
  • 30
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.

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.

Martin Jespersen
  • 25,743
  • 8
  • 56
  • 68
  • It works exactly like it should as you can see in this fiddle: http://jsfiddle.net/cWshc/2/ - if you can't make it work you are probably not setting the right path or domain or similar. – Martin Jespersen Sep 23 '11 at 08:19
  • 1
    same issue for me. Chrome is not deleting cookies when i run the clearCookie function provided. – Justin Carlson Apr 01 '12 at 10:18
  • For me this works if I use .toGMTString() (I've not tried toUTCString()) – iainbeeston Sep 14 '12 at 05:25
  • 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) – Martin Jespersen Sep 14 '12 at 06:31
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