JavaScript code for cookie not working in Chrome
Solution 1
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
Solution 2
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.
Related videos on Youtube
user961627
Updated on February 22, 2020Comments
-
user961627 about 4 years
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, thec_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. -
user961627 about 11 yearsbut the
orderdetailid
value is only an integer. -
sbgoran about 11 yearsWell just in case :) Do you have cookies enabled in your Chrome browser?
-
Chad about 11 yearsConfirmed that this works fine for me in Chrome 25 as well. Even when I leave the
$('#orderdetailid').val()
so that it isundefined
, it still works. -
user961627 about 11 yearsHmm that's strange, then how are cookies accessed in Chrome? When I go to "Console" - the last option on the left (not Resources), then when I type document.cookie, the cookie isn't there. I did the same with the fiddle you linked me to, but while it is there under
Cookies > fiddle.jshell.net
it's not there indocument.cookie
. Try typing this in the console:alert(document.cookie);
-
alexander.biskop about 11 yearsThis is probably a cross-site issue. In the JSFiddle example, the script setting the cookie is loaded from fiddle.jsshell.net. Therefore, the cookie is also set for this domain. When typing document.cookie in the dev console, you will only see the cookies for domain jsfiddle.net. Could this also be the problem with your scenario? I.e. the domain of the script that creates the cookie and the domain it should be used/read in must be the same.
-
Chad about 11 yearsWhen you do
document.cookies
it shows the cookies for the domain you are on which isjsfiddle.net
, the code you write on a fiddle runs in an iframe hosted atfiddle.jshell.net
so it will not show up indocument.cookie
. Look in resources under the proper location and you can see it is working fine. -
Dov Benyomin Sohacheski over 6 yearsYou should elaborate, this is a very weak answer.
-
ds_secret about 6 yearsWhy does only Chrome require you to save the cookies with a domain and http communication when IE and Edge don't.
-
Roman Hocke about 6 years@ds_secret: I tested this in IE8 and it behaved the same way as Chrome did - on "file://" it did not save the cookies, while on "http://" it did. On the other hand, FF seems to have some workaround for this, it does save the cookies even on "file://".
-
ds_secret about 6 yearsIs there a workaround for this, as most of my "clients" use Chrome, not Edge, IE, or Firefox?