views:

3741

answers:

3

I am using the jQuery Cookie plugin (download and demo and source code with comments) to set and read a cookie. I'm developing the page on my local machine.

The following code will successfully set a cookie in FireFox 3, IE 7, and Safari (PC). But if the browser is Google Chrome AND the page is a local file, it does not work.

$.cookie("nameofcookie", cookievalue, {path: "/", expires: 30});

What I know:

  • The plugin's demo works with Chrome.
  • If I put my code on a web server (address starting with http://), it works with Chrome.

So the cookie fails only for Google Chrome on local files.

Possible causes:

  • Google Chrome doesn't accept cookies from web pages on the hard drive (paths like file:///C:/websites/foo.html)
  • Something in the plugin implentation causes Chrome to reject such cookies

Can anyone confirm this and identify the root cause?

A: 

Another possible cause is the path: "/", since you're not using a normal web URL, / probably doesn't mean much - try without setting the path at all.

Greg
Good thought, but '/' is the default anyway. I tried 'file:///C:/' but I think that's nonsense in this context.
Nathan Long
+13  A: 

Chrome doesn't support cookies for local files unless you start it with the --enable-file-cookies flag. You can read a discussion about it at http://code.google.com/p/chromium/issues/detail?id=535.

Matthew Crumley
While I am not about to rush in and use Chrome, I do use TiddlyWiki daily and I was surprised at the response from the Chrome team over this "issue".
Peter M
Awesome! Way to track down the root cause - and a solution.
Nathan Long
+2  A: 

For local applications use localStorage in Chrome instead: http://people.w3.org/mike/localstorage.html

Yuri