【问题标题】:Cookie expiration dateCookie 到期日期
【发布时间】:2011-12-01 09:58:16
【问题描述】:

我不是程序员。我正在尝试使用一个 cookie 脚本来记住最后的下拉菜单选择。

我找到了一个有效的脚本,但它只做一个会话 cookie。如何在此脚本中为 cookie 添加过期日期?

<head>
  <script>        
    function SETcookie() {
      document.cookie = "Selected=" + document.getElementById('myList').selectedIndex;
    }

    function GETcookie() {
      if (document.cookie) {
        eval(document.cookie);
        document.getElementById('myList').selectedIndex = Selected;
      }
    }    
  </script>
</head>

<body onLoad="GETcookie()">
  <select id="myList" onChange="SETcookie()">
    <option value="1">Option 1</option>
    <option value="2">Option 2</option>
    <option value="3">Option 3</option>
    <option value="4">Option 4</option>
  </select>
</body>

【问题讨论】:

    标签: javascript cookies


    【解决方案1】:

    试试这个:

    function setCookie(c_name,c_value,exdays) {
       var exdate=new Date();
       exdate.setDate(exdate.getDate() + exdays);
       document.cookie=encodeURIComponent(c_name) 
         + "=" + encodeURIComponent(c_value)
         + (!exdays ? "" : "; expires="+exdate.toUTCString());
         ;
    }
    

    c_name 是 cookie 的名称

    c_value 是 cookie 值

    exdays 是您希望 cookie 过期的天数

    来源:http://www.w3schools.com/js/js_cookies.asp

    【讨论】:

      【解决方案2】:

      这是 100% 正常工作且没有折旧功能的功能。

      function setCookie(variable, value, expires_seconds) {
          var d = new Date();
          d = new Date(d.getTime() + 1000 * expires_seconds);
          document.cookie = variable + '=' + value + '; expires=' + d.toGMTString() + ';';
      }
      

      【讨论】:

        【解决方案3】:

        也许这会有所帮助

        document.cookie = "coolName"+ "=" +"coolValue"+ ";" + "expires="+ new Date(new Date().getTime()+60*60*1000*24).toGMTString()+";path=/";
        

        【讨论】:

        • 这会将到期时间设置为一天,这可能是您想要的...大多数情况下,我通过设置使用会话 cookie/存储或“永久”存储有效期至 10 年。
        【解决方案4】:

        ;max-age=max-age-in-seconds(例如,606024*365 或 31536000 为一年) ;expires=date-in-GMTString-format 如果既没有过期也没有指定 max-age,它将在会话结束时过期。

        document.cookie = "doSomethingOnlyOnce=true; expires=Fri, 31 Dec 9999 23:59:59 GMT; SameSite=None; Secure";
        

        know more

        【讨论】:

          【解决方案5】:

          试试

          var a = new Date();
          a = new Date(a.getTime() +1000*60*60*24*365);
          document.cookie = 'mycookie=somevalue; expires='+a.toGMTString()+';'; 
          

          PS。值 1000*60*60*24*365 = 1 年

          要获取选定的索引,试试这个 GETcookie:

          function GETcookie(){    
          if (document.cookie){    
          var a = document.cookie;
          Selected = a.substring(a.search('Selected=')+9,a.search(';'));
          alert("Selected = " + Selected);
          document.getElementById('myList').selectedIndex=Selected;
          }}
          

          【讨论】:

          • 那行不通。也许我把它放错了地方。你能告诉我它应该怎么在这里吗jsfiddle.net/BrUmu
          • 只需在您的 SETcookie 函数中设置它。你得到了什么?
          • 我做到了,cookie 完全停止工作。在此处查看更新jsfiddle.net/BrUmu/1
          【解决方案6】:

          你可以试试这个:

          function SETcookie(){  
            var validity_days = 7;
            var expires = validity_days * 1000 * 60 * 60 * 24;
            var expires_date = new Date( today.getTime() + (expires) );
            document.cookie="Selected="+document.getElementById('myList').selectedIndex + ";expires=" + expires_date.toGMTString() + ";";
          }
          

          【讨论】:

          • "today" 未定义,您的函数仅适用于您自己的代码,因为它使用#myList DOM 对象。它有问题而且没用。
          • 你说得对,这里没有定义“今天”,但“myList”是问题中元素的 id,这不是他们刚刚编造的。
          【解决方案7】:

          这是用于以天为单位设置到期日期(此处为 5 天)

          function setCookie(cname, cvalue, exdays) {     
            var d = new Date();
            d.setTime(d.getTime() + exdays * 24 * 60 * 60 * 1000);
            var expires = "expires="+d.toUTCString();
          
          }
          
          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 user = getCookie("username");
            if (user != "") {
               //your code goes here
            } else {
               //your code goes here
              if (user != "" && user != null) {
                setCookie("username", user, 5);
              }
            }
          }
          

          【讨论】:

            【解决方案8】:

            这是用于以分钟为单位设置到期日期(此处为 5 分钟)

            function setCookie(cname, cvalue, exdays) {     
              var d = new Date();
              d.setTime(d.getTime() + exdays * 60 * 1000);
              var expires = "expires="+d.toUTCString();
              
            }
            
            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 user = getCookie("username");
              if (user != "") {
                 //your code goes here
              } else {
                 //your code goes here
                if (user != "" && user != null) {
                  setCookie("username", user, 5);
                }
              }
            }
            

            【讨论】:

              猜你喜欢
              • 2011-06-19
              • 2017-12-02
              • 2011-04-23
              • 1970-01-01
              • 1970-01-01
              • 2010-11-16
              • 1970-01-01
              • 2017-11-10
              相关资源
              最近更新 更多