最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

JavaScript cookie not saving - Stack Overflow

programmeradmin0浏览0评论

I am having a trouble with JavaScript cookie. I run this code last time and it worked like what I want to. It shows a prompt letting me to enter my name, and clicks OK after entering my name. It also worked when I refreshed the page and shows an alert message saying " Wele again Mark". But now, this code does not work anymore. It only ask and ask my name everytime I refresh the page. I'm doubting if it's on my browser I already tries tor un this code in IE, Firefox, Chrome, and MS Edge but it does the same thing. Hope you cans olve the problem guys. Thanks. The code is below:

<html>
  <head>
    <script type="text/javascript">
      function setCookie(cname, cvalue, exdays)
      {
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toGMTString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
      }
      function getCookie(cname)
      {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i = 0; i < ca.length; i++)
        {
          var c = ca[i];
          while(c.charAt(0) == ' ')
          {
            c = c.substring(1);
          }
          if(c.indexOf(name) == 0)
          {
            return c.substring(name.length,c.length);
          }
        }
        return "";
      }
      function checkCookie()
      {
        var username = getCookie("username");
        if(username != null && username != "")
        {
          alert("Wele again " + username);
        } else
        {
          username = prompt("Please enter your name:" , "");
          if(username != null && username != "")
          {
            setCookie("username" , username , 365);
          }
        }
      }
   </script>
 </head>
 <body onload="checkCookie()">
 </body>
 </html>

I am having a trouble with JavaScript cookie. I run this code last time and it worked like what I want to. It shows a prompt letting me to enter my name, and clicks OK after entering my name. It also worked when I refreshed the page and shows an alert message saying " Wele again Mark". But now, this code does not work anymore. It only ask and ask my name everytime I refresh the page. I'm doubting if it's on my browser I already tries tor un this code in IE, Firefox, Chrome, and MS Edge but it does the same thing. Hope you cans olve the problem guys. Thanks. The code is below:

<html>
  <head>
    <script type="text/javascript">
      function setCookie(cname, cvalue, exdays)
      {
        var d = new Date();
        d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
        var expires = "expires=" + d.toGMTString();
        document.cookie = cname + "=" + cvalue + "; " + expires;
      }
      function getCookie(cname)
      {
        var name = cname + "=";
        var ca = document.cookie.split(';');
        for(var i = 0; i < ca.length; i++)
        {
          var c = ca[i];
          while(c.charAt(0) == ' ')
          {
            c = c.substring(1);
          }
          if(c.indexOf(name) == 0)
          {
            return c.substring(name.length,c.length);
          }
        }
        return "";
      }
      function checkCookie()
      {
        var username = getCookie("username");
        if(username != null && username != "")
        {
          alert("Wele again " + username);
        } else
        {
          username = prompt("Please enter your name:" , "");
          if(username != null && username != "")
          {
            setCookie("username" , username , 365);
          }
        }
      }
   </script>
 </head>
 <body onload="checkCookie()">
 </body>
 </html>
Share Improve this question edited Dec 28, 2017 at 9:06 Mark Cay asked Dec 28, 2017 at 8:33 Mark CayMark Cay 331 silver badge10 bronze badges 4
  • Any error in console? – Frederik.L Commented Dec 28, 2017 at 8:35
  • I am sorry, I do not quietly understand it. You mean if Console works properly in my browser? – Mark Cay Commented Dec 28, 2017 at 8:38
  • I meant if there was any errors showing up in the console when you open the page. Also, in your code why do you define a variable d in setCookie if you don't use it afterwards? Is it meant to be used for expires ? – Frederik.L Commented Dec 28, 2017 at 9:02
  • Actually it's: var expires = "expires=" + d.toGMTString(); – Mark Cay Commented Dec 28, 2017 at 9:04
Add a ment  | 

2 Answers 2

Reset to default 3
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 
1000));
var expires = "expires=";
document.cookie = cname + "=" + cvalue + "; " + 
expires;

Your cookie string doesn't include the date. You forgot the + d.

Your issue is probably this line:

document.cookie = cname + "=" + cvalue + "; " +
            expires;

You forgot adding the date:

document.cookie = cname + "=" + cvalue + "; " +
            expires + d;
发布评论

评论列表(0)

  1. 暂无评论