Cookie过期日期

21

我不是程序员。我试图使用一个可以记住最后下拉菜单选择的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>
8个回答

16

试试这个:

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


9
也许这会有所帮助。
document.cookie = "coolName"+ "=" +"coolValue"+ ";" + "expires="+ new Date(new Date().getTime()+60*60*1000*24).toGMTString()+";path=/";

这将把过期时间设置为一天,这可能是您想要的...大多数情况下,我要么使用会话cookie/存储,要么通过将过期时间设置为10年来使用“永久”存储。 - Stijn de Witt

8
这是一个功能完全可用且没有过时函数的函数。
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() + ';';
}

6
尝试
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;
}}

那个不起作用。也许我放错了地方。你能展示一下应该放在这里吗 http://jsfiddle.net/BrUmu/ - mr.data1
只需在您的SETcookie函数中设置这个。你得到什么? - CloudyMarble
我做了一些更改,结果cookie完全停止工作。请参见此处的更新http://jsfiddle.net/BrUmu/1/。 - mr.data1

1

;max-age=max-age-in-seconds(例如,606024*365或31536000表示一年) ;expires=GMTString格式的日期。如果未指定expires和max-age,则会在会话结束时过期。

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

了解更多


0

这是用于以分钟为单位设置到期日期(这里是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);
    }
  }
}

0

这是用于以天为单位设置到期日期(此处为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);
    }
  }
}

0
你可以尝试这个:
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() + ";";
}

1
“today”未定义,而且你的函数只适用于你自己的代码,因为它使用了#myList DOM对象。它有缺陷且无用。 - Lukas Liesis
你说得对,这里没有定义“today”,但是“myList”是问题中元素的ID,而不是他们随意编造的。 - Aidy J

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接