cookie 的 JavaScript 代码在 Chrome 中不起作用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/15385641/
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
JavaScript code for cookie not working in Chrome
提问by user961627
The following code works fine in FF:
以下代码在 FF 中工作正常:
var date = new Date();
date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
document.cookie = "c_odi" + "=" + $('#orderdetailid').val() + expires + "; path=/";
But not in Chrome. When I'm using Chrome and I do document.cookie
in the console to view cookies, the c_odi
cookie isn't there. But when I do the same in FF, it is. How can we make cookies work in Chrome? The cookies that were added by PHP are fine, but not this one in JavaScript, and I do need to add this cookie via JavaScript at this point.
但不是在 Chrome 中。当我使用 Chrome 并document.cookie
在控制台中查看 cookie 时,c_odi
cookie 不存在。但是当我在 FF 中做同样的事情时,它是。我们如何让 cookie 在 Chrome 中工作?PHP 添加的 cookie 很好,但在 JavaScript 中没有这个,此时我确实需要通过 JavaScript 添加此 cookie。
回答by Roman Hocke
This problem can occur if You open Your code as file:///C:/.../xxx.html
instead of http:// localhost/xxx.html
. Chrome doesn't save cookies (because there is no domain and no http communication) in file://
case.
如果您打开您的代码file:///C:/.../xxx.html
而不是http:// localhost/xxx.html
. Chrome 不会保存 cookie(因为没有域,也没有 http 通信)file://
以防万一。
Few links of interest:
几个感兴趣的链接:
回答by sbgoran
Try to replace this line:
尝试替换此行:
document.cookie = "c_odi" + "=" + $('#orderdetailid').val() + expires + "; path=/";
with this one:
有了这个:
document.cookie = "c_odi" + "=" + escape($('#orderdetailid').val()) + expires + "; path=/";
You would have to use unescape
when you try to read value, but you'll menage when time comes :)
unescape
当您尝试读取价值时,您将不得不使用,但您会在时间到时进行管理:)
回答by alexander.biskop
Seems like it's working for me:
似乎它对我有用:
At least the cookie shows up in dev tools, as you can see. However, I replaced the jQuery selector $('#orderdetailid').val()
with a constant value, as you can see. Is there something wrong with that value or the element containing the value maybe?
如您所见,至少 cookie 出现在开发工具中。但是,$('#orderdetailid').val()
如您所见,我用常量值替换了 jQuery 选择器。该值或包含该值的元素是否有问题?
回答by Dean
Make sure your address bar url matches the domain. In Chrome if you set domain=www.site.com and then test your page in the browser missing out the www. it won't work.
确保您的地址栏 url 与域匹配。在 Chrome 中,如果您设置 domain=www.site.com,然后在浏览器中测试您的页面,但会遗漏 www。它不会工作。
回答by Deepak Ranganathan
Chrome doesn't store cookies from the pages which are loaded from local file system. For example if you are accessing a HTML file in chrome browser from local file system(ex: file:///C:/Users/deepak.r/Desktop/test.html), cookies are not supported.
Chrome 不会存储来自本地文件系统加载的页面的 cookie。例如,如果您从本地文件系统(例如:file:///C:/Users/deepak.r/Desktop/test.html)访问 Chrome 浏览器中的 HTML 文件,则不支持 cookie。