Cannot set cookies in Javascript

后端 未结 9 2124
青春惊慌失措
青春惊慌失措 2020-12-01 10:28

I have a very simple line of code that set and read a cookie. I kept getting empty value for my cookie and have no understanding why. I have cookie enabled and know that coo

相关标签:
9条回答
  • 2020-12-01 11:05

    I get the same weird behavior when opening a page in Chrome from localhost

    When I map the same page in my hosts file and open the same page through the mapped name, normal cookie functionality resumes.

    1. Open you hosts file as admin. (usually c:\windows\system32\drivers\etc\hosts)
    2. Add 127.0.0.1 localhostdevelopment.com or similar to the end of your hosts file
    3. Save your hosts file (will be rejected if you did not open the file as admin)
    4. Go to http://localhostdevelopment.com/ (or whatever you called it) in Chrome.
    5. Please enjoy having your cookies behave normally.
    0 讨论(0)
  • 2020-12-01 11:06

    Chrome denies file cookies. To make your program work, you going to have to try it in a different browser or upload it to a remote server. Plus, the code for your setcookie and getcookie is essentially wrong. Try using this to set your cookie:

    function setCookie(name,value,expires){
       document.cookie = name + "=" + value + ((expires==null) ? "" : ";expires=" + expires.toGMTString())
    }
    

    example of usage:

    var expirydate=new Date();
    expirydate.setTime( expirydate.getTime()+(100*60*60*24*100) )
    setCookie('cookiename','cookiedata',expirydate)
    // expirydate being a variable with the expiry date in it
    // the one i have set for your convenience expires in 10 days
    

    and this to get your cookie:

    function getCookie(name) {
       var cookieName = name + "="
       var docCookie = document.cookie
       var cookieStart
       var end
    
       if (docCookie.length>0) {
          cookieStart = docCookie.indexOf(cookieName)
          if (cookieStart != -1) {
             cookieStart = cookieStart + cookieName.length
             end = docCookie.indexOf(";",cookieStart)
             if (end == -1) {
                end = docCookie.length
             }
             return unescape(docCookie.substring(cookieStart,end))
          }
       }
       return false
    }
    

    example of usage:

    getCookie('cookiename');
    

    Hope this helps.

    Cheers, CoolSmoothie

    0 讨论(0)
  • 2020-12-01 11:08

    With chrome, you cannot create cookies on a local website, so you need to trick your browser into thinking this site is not local by doing the following:

    1) Place the root directory of the web site into C:\inetpub\wwwroot, (so it looks like C:\inetpub\wwwroot\yourProjectFolder)

    2) Get your computer name by right clicking Computer, clicking properties, and looking for Computer Name

    3) Access your site in the browser by visiting http://my-computer-name/yourProjectFolder/index.html, at which point creating cookies should work.

    (notice that I use dashes in the computer name, NOT underscores)

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