Why does Chrome ignore local jQuery cookies?

2018-12-31 22:41发布

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?

8条回答
其实,你不懂
2楼-- · 2018-12-31 22:56

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

查看更多
步步皆殇っ
3楼-- · 2018-12-31 22:58

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.

查看更多
若你有天会懂
4楼-- · 2018-12-31 22:58

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.

查看更多
残风、尘缘若梦
5楼-- · 2018-12-31 22:58

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 !

查看更多
像晚风撩人
6楼-- · 2018-12-31 23:01

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.

查看更多
唯独是你
7楼-- · 2018-12-31 23:02

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

查看更多
登录 后发表回答