Javascript Cookie 有效期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8339406/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-24 05:37:08  来源:igfitidea点击:

Cookie expiration date

javascriptcookies

提问by mr.data1

I am not a programmer. I am trying to use a cookie script that remembers the last drop down menu selection.

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

I found a script that works but it does only a session cookie. How do I add an expiration date to the cookie in this script?

我找到了一个有效的脚本,但它只执行会话 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>

回答by vimist

Try this:

尝试这个:

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_nameis the name of the cookie

c_name是 cookie 的名称

c_valueis the cookie value

c_value是 cookie 值

exdaysis the number of days you want the cookie to expire after

exdays是您希望 cookie 过期的天数

Source: http://www.w3schools.com/js/js_cookies.asp

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

回答by Lukas Liesis

Here's function which is 100% working and has no depreciated functions.

这是 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() + ';';
}

回答by Kirill Shur

May be this will help

可能这会有所帮助

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

回答by CloudyMarble

try

尝试

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

PS. The value 1000*60*60*24*365 = 1 Year

附注。值 1000*60*60*24*365 = 1 年

To Get the selected index try this GETcookie:

要获取选定的索引,请尝试使用此 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;
}}

回答by loscuropresagio

You could try this:

你可以试试这个:

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() + ";";
}

回答by Roshini Dasari

This is for setting the expiry date in terms of minutes(5 minutes here)

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);
    }
  }
}

回答by Roshini Dasari

This is for setting the expiry date in terms of days(5 days here)

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);
    }
  }
}