简体   繁体   中英

Chrome not setting cookie path to root

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM