Cookie代码不适用于Chrome [英] JavaScript code for cookie not working in Chrome
问题描述
以下代码在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 = /;
但不是在Chrome中。当我使用Chrome浏览器并在控制台中执行 document.cookie
查看cookie时, c_odi
cookie不是'那里。但是当我在FF中也这样做时,情况就是如此。我们如何让Cookie在Chrome中工作?由PHP添加的cookies很好,但在JavaScript中没有这个,我确实需要通过JavaScript添加此cookie。
如果您将代码打开为 file:/// C:/.../xxx.html
而不是<$ c $,则可能会出现此问题c> http:// localhost / xxx.html 。 Chrome不会在 file://
情况下保存cookie(因为没有域名,也没有http通信)。
少数感兴趣的链接:
The following code works fine in 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.
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.
Few links of interest:
- https://gist.github.com/shellscape/02d3a97031e7afdf99d2642f93d59486
- Setting Cookies using JavaScript in a local html file
- https://bugzilla.mozilla.org/show_bug.cgi?id=536650
- https://datatables.net/forums/discussion/46255/save-state-to-cookie-in-file-protocol
这篇关于Cookie代码不适用于Chrome的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!