7

I am setting a cookie in Javascript using the following code :

setCookie('cart_items','product_name');


function setCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

But the cookie path is not set to root (/) in Chrome. Instead it gets set to the path from where the web page is being executed !!

I tested with IE and FF. It works fine with both these browsers ....

What might be wrong with Chrome or Is it the problem with cookie creation code i am using??

In Chrome ( 16.0.912.63 )

Path: /xxxxxxxx/xxxxxxx

in FF ( 6.0 )

Path: /

in IE (9)

Path: /

Sandy505
  • 888
  • 3
  • 15
  • 26

1 Answers1

10

The reason this happens is because chrome doesn't allow setting cookies on local files by default. See this answer for more information: https://stackoverflow.com/a/347997/1324019 (text from answer)

Chrome doesn't support cookies for local files (or, like Peter Lyons mentioned, localhost*) unless you start it with the --enable-file-cookies flag. You can read a discussion about it at http://code.google.com/p/chromium/issues/detail?id=535.

*Chrome does support cookies if you use the local IP address (127.0.0.1) directly. so in the localhost case, that could be an easier workaround.

Community
  • 1
  • 1
Mansfield
  • 14,445
  • 18
  • 76
  • 112