为什么 Chrome 会忽略本地 jQuery cookie?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/335244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Why does Chrome ignore local jQuery cookies?
提问by Nathan Long
I am using the jQuery Cookie plugin (downloadand demoand source code with comments) to set and read a cookie. I'm developing the page on my local machine.
我正在使用 jQuery Cookie 插件(下载和演示以及带有注释的源代码)来设置和读取 cookie。我正在我的本地机器上开发页面。
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.
以下代码将在 FireFox 3、IE 7 和 Safari (PC) 中成功设置 cookie。但是如果浏览器是 Google Chrome 并且页面是本地文件,则它不起作用。
$.cookie("nameofcookie", cookievalue, {path: "/", expires: 30});
What I know:
我所知道的:
- The plugin's demoworks with Chrome.
- If I put my code on a web server (address starting with http://), it works with Chrome.
- 该插件的演示适用于 Chrome。
- 如果我将代码放在网络服务器上(地址以 http:// 开头),它可以与 Chrome 一起使用。
So the cookie fails only for Google Chrome on local files.
因此 cookie 仅对本地文件上的 Google Chrome失败。
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
- 谷歌浏览器不接受来自硬盘驱动器网页的 cookie(路径如 file:///C:/websites/foo.html)
- 插件实现中的某些内容导致 Chrome 拒绝此类 cookie
Can anyone confirm this and identify the root cause?
任何人都可以确认这一点并确定根本原因吗?
回答by Matthew Crumley
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 不支持本地文件的 cookie(或者,像 Peter Lyons 提到的,localhost*),除非你用 --enable-file-cookies 标志启动它。您可以在http://code.google.com/p/chromium/issues/detail?id=535阅读有关它的讨论。
*Chrome doessupport cookies if you use the local IP address (127.0.0.1) directly. so in the localhost case, that could be an easier workaround.
*如果您直接使用本地 IP 地址 (127.0.0.1),Chrome确实支持 cookie。所以在本地主机的情况下,这可能是一个更简单的解决方法。
回答by Yuri
For local applications use localStorage in Chrome instead: http://people.w3.org/mike/localstorage.html
对于本地应用程序,请在 Chrome 中使用 localStorage:http: //people.w3.org/mike/localstorage.html
回答by Serdar Güner
i had some problem and solved it this terrible solution. using store and cookie plugin together.
我遇到了一些问题并解决了这个可怕的解决方案。一起使用 store 和 cookie 插件。
<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
我知道这不是完美的解决方案,但对我有用
回答by paulalexandru
回答by Tiji
I had the same issue, please try using the IP address of localhost instead. For e.g "http://127.0.0.1/yoursite/"
我遇到了同样的问题,请尝试使用 localhost 的 IP 地址。例如“http://127.0.0.1/yoursite/”
回答by chaoxfu
please check out Cookies & Google Analytics.
请查看Cookies 和 Google Analytics。
$.cookie("nameofcookie", cookievalue, {path: "/", expires: 30});
change this line to
将此行更改为
$.cookie("nameofcookie", cookievalue, {*Path:* "/", expires: 30});
this project working is fine.
这个项目工作正常。
回答by Greg
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.
另一个可能的原因是path: "/"
,因为您没有使用普通的 Web URL,所以/
可能没有太大意义 - 尝试根本不设置路径。
回答by Elyes Frikha
If you use chrominum this is the command to enable local cookies
如果您使用 chrominum,这是启用本地 cookie 的命令
chromium-browser --enable-file-cookies
铬浏览器 --enable-file-cookies
It's the same thing for chrome
铬也是一样的
Hope this help you !
希望这对你有帮助!
回答by Yaroslav Wally
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
作为解决方法,您可以使用 Tampermonkey 访问本地文件(如何在 Tampermonkey 脚本中包含本地 htm 页面?)通过这种方式,您将使用 Tampermonkey 的存储,并且将能够通过函数 GM_getValue(data) 和GM_setValue(数据)。我将它用于我的本地 HTML 页面,我将其用作 Windows 资源管理器的可自定义替代品
But actually localStoragefrom Yuri's answerworks perfect.
但实际上,尤里的回答中的localStorage效果很好。