Chrome not setting cookie path to root

10,334

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.

Share:
10,334
Sandy505
Author by

Sandy505

Updated on June 18, 2022

Comments

  • Sandy505
    Sandy505 almost 2 years

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

  • duggi
    duggi almost 9 years
    ^ above is correct answer. if only OP would mark it as such