Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Javascript cookie removal not working on Chrome

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?

like image 770
Qwerky Avatar asked Nov 11 '10 11:11

Qwerky


People also ask

Why can't I delete cookies?

Moreover, if you try to remove the Google services cookies, the browser will automatically re-create them, making it impossible to remove data stored on Google servers. Thankfully, you can fix it by logging out of the Google services and then clearing out the browser history.

How can a cookie be deleted using JavaScript?

You can use setCookie('name', 'value', 0) to delete a cookie.

Can JavaScript manipulate cookies?

JavaScript can also manipulate cookies using the cookie property of the Document object. JavaScript can read, create, modify, and delete the cookies that apply to the current web page.


3 Answers

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

like image 100
scaryman Avatar answered Oct 04 '22 03:10

scaryman


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.

like image 38
Martin Jespersen Avatar answered Oct 04 '22 04:10

Martin Jespersen


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.

like image 27
deadman1204 Avatar answered Oct 04 '22 05:10

deadman1204