最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

JavaScript cookie not saving - Stack Overflow

matteradmin5PV0评论

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;

Articles related to this article

Post a comment

comment list (0)

  1. No comments so far