JavaScript code for cookie not working in Chrome

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:

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

Seems like it's working for me:

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?

Make sure your address bar url matches the domain. In Chrome if you set and then test your page in the browser missing out the www. it won't work.

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.

Need Your Help

ORA-06502 from query that works in standalone SQL

sql oracle plsql ora-06502

I am getting the ORA-06502 error while calling this query from PL/SQL.