JavaScript code for cookie not working in Chrome

38,170

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:

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.

Share:
38,170

Related videos on Youtube

user961627
Author by

user961627

Updated on February 22, 2020

Comments

  • user961627
    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, 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.

  • user961627
    user961627 about 11 years
    but the orderdetailid value is only an integer.
  • sbgoran
    sbgoran about 11 years
    Well just in case :) Do you have cookies enabled in your Chrome browser?
  • Chad
    Chad about 11 years
    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.
  • user961627
    user961627 about 11 years
    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);
  • alexander.biskop
    alexander.biskop about 11 years
    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.
  • Chad
    Chad about 11 years
    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.
  • Dov Benyomin Sohacheski
    Dov Benyomin Sohacheski over 6 years
    You should elaborate, this is a very weak answer.
  • ds_secret
    ds_secret about 6 years
    Why does only Chrome require you to save the cookies with a domain and http communication when IE and Edge don't.
  • Roman Hocke
    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
    ds_secret about 6 years
    Is there a workaround for this, as most of my "clients" use Chrome, not Edge, IE, or Firefox?