Why does Chrome ignore local jQuery cookies?
Asked Answered
C

9

85

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?

Clientele answered 2/12, 2008 at 20:6 Comment(2)
When you say the demo works, do you mean if you save it to your hard drive and access it like file://?Threlkeld
No - just accessing the link above with Chrome.Clientele
U
97

Chrome doesn't support cookies for local files (or, like Peter Lyons mentioned, localhost*) 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.

*Chrome does support cookies if you use the local IP address (127.0.0.1) directly. so in the localhost case, that could be an easier workaround.

Upolu answered 7/12, 2008 at 20:0 Comment(2)
Setting the domain to the IP did not work for me, but it worked if I removed the domain (locally, we need the domain for production)Characharabanc
On Mac OSX to start Chrome with the flag --enable-file-cookies use a terminal comnad: 'open /Applications/Google\ Chrome.app/Contents/MacOS/Google\ Chrome --args --enable-file-cookies' - (or tweak it according to your Chrome.app location)Isosceles
D
7

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

Disrate answered 20/3, 2010 at 16:15 Comment(0)
L
6

i had some problem and solved it this terrible solution. using store and cookie plugin together.

<script src="js/jquery.cookies.2.2.0.js" type="text/javascript"></script>
<script src="js/jquery.Storage.js" type="text/javascript"></script>

var is_chrome = navigator.userAgent.toLowerCase().indexOf('chrome') > -1;

//get cookies
var helpFlag=(is_chrome)?$.Storage.get("helpFlag"):$.cookies.get("helpFlag");

//set cookies
if(is_chrome)$.Storage.set("helpFlag", "1");else $.cookies.set("helpFlag", "1");

I know that this isnt perfect solution but works for me

Lalla answered 7/10, 2011 at 9:39 Comment(0)
P
6

This did the job for me:

enter image description here

Right click on your Chrome Icon and select Properties, Shortcut tab and add --enable-file-cookies at the last of the target path.

Phylloid answered 16/5, 2017 at 8:28 Comment(0)
D
3

I had the same issue, please try using the IP address of localhost instead. For e.g "http://127.0.0.1/yoursite/"

Detroit answered 24/6, 2011 at 13:25 Comment(0)
H
3

please check out Cookies & Google Analytics.

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

change this line to

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

this project working is fine.

Haiti answered 21/9, 2011 at 4:7 Comment(1)
Thanks, changing the case to "Path" fix my problemPulchritudinous
T
2

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.

Threlkeld answered 2/12, 2008 at 20:9 Comment(1)
Good thought, but '/' is the default anyway. I tried 'file:///C:/' but I think that's nonsense in this context.Clientele
N
0

If you use chrominum this is the command to enable local cookies

chromium-browser --enable-file-cookies

It's the same thing for chrome

Hope this help you !

Neel answered 13/5, 2015 at 16:29 Comment(0)
E
0

As workaround you can use Tampermonkey with access to local files ( How to include Local htm pages in a Tampermonkey script? ) By that way you will use Tampermonkey's storage, and will be able to set and get your data by functions GM_getValue(data) and GM_setValue(data). I used that for my local HTML page, which i used as customizable alternative to Windows Explorer

But actually localStorage from Yuri's answer works perfect.

Eternize answered 30/3, 2020 at 14:45 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.