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.

6 Answers 6

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:

Sign up to request clarification or add additional context in comments.

3 Comments

Why does only Chrome require you to save the cookies with a domain and http communication when IE and Edge don't.
@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://".
Is there a workaround for this, as most of my "clients" use Chrome, not Edge, IE, or Firefox?
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.

1 Comment

Image
You should elaborate, this is a very weak answer.
1

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");

Comments

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

2 Comments

but the orderdetailid value is only an integer.
Well just in case :) Do you have cookies enabled in your Chrome browser?
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?

4 Comments

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.
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);
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.
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.