23

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.

informatik01
  • 16,038
  • 10
  • 74
  • 104
user961627
  • 12,379
  • 42
  • 136
  • 210

6 Answers6

55

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:

Roman Hocke
  • 4,137
  • 1
  • 20
  • 34
  • Why does only Chrome require you to save the cookies with a domain and http communication when IE and Edge don't. – ds_secret Feb 22 '18 at 01:30
  • @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://". – Roman Hocke Feb 22 '18 at 08:25
  • Is there a workaround for this, as most of my "clients" use Chrome, not Edge, IE, or Firefox? – ds_secret Feb 23 '18 at 01:22
4

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.

0

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 :)

sbgoran
  • 3,451
  • 2
  • 19
  • 30
0

Seems like it's working for me:

enter image description here

http://jsfiddle.net/rQEnF/3/

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?

alexander.biskop
  • 1,822
  • 19
  • 30
  • Confirmed that this works fine for me in Chrome 25 as well. Even when I leave the `$('#orderdetailid').val()` so that it is `undefined`, it still works. – Chad Mar 13 '13 at 13:07
  • Hmm 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 in `document.cookie`. Try typing this in the console: `alert(document.cookie);` – user961627 Mar 13 '13 at 13:16
  • This 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. – alexander.biskop Mar 13 '13 at 13:24
  • When you do `document.cookies` it shows the cookies for the domain you are on which is `jsfiddle.net`, the code you write on a fiddle runs in an iframe hosted at `fiddle.jshell.net` so it will not show up in `document.cookie`. Look in resources under the proper location and you can see it is working fine. – Chad Mar 13 '13 at 14:32
0

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.

Dean
  • 1
0

When opening HTML files in chrome as file://, as a workaround, use the session storage instead of cookies. Session storage does not have the exact same functionality as cookies of course, but it may be helpful in some cases.

sessionStorage.setItem("some_property", "true"); sessionStorage.getItem("some_property");