JavaScript cookie not saving - Stack Overflow

I am having a trouble with JavaScript cookie. I run this code last time and it worked like what I want

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;

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1745646513a4638002.html

相关推荐

  • JavaScript cookie not saving - Stack Overflow

    I am having a trouble with JavaScript cookie. I run this code last time and it worked like what I want

    5小时前
    10

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信