JavaScript code for cookie not working in Chrome

后端 未结 5 1306
谎友^
谎友^ 2021-01-01 16:59

The following code works fine in FF:

var date = new Date();
date.setTime(date.getTime() + (1 * 24 * 60 * 60 * 1000));
expires = \"; expires=\" + date.toGMTSt         


        
相关标签:
5条回答
  • 2021-01-01 17:04

    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?

    0 讨论(0)
  • 2021-01-01 17:19

    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:

    • https://gist.github.com/shellscape/02d3a97031e7afdf99d2642f93d59486
    • Setting Cookies using JavaScript in a local html file
    • https://bugzilla.mozilla.org/show_bug.cgi?id=536650
    • https://datatables.net/forums/discussion/46255/save-state-to-cookie-in-file-protocol
    0 讨论(0)
  • 2021-01-01 17:20

    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.

    0 讨论(0)
  • 2021-01-01 17:26

    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.

    0 讨论(0)
  • 2021-01-01 17:29

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

    0 讨论(0)
提交回复
热议问题