Cookie过期日期 [英] Cookie expiration date

查看:100
本文介绍了Cookie过期日期的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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

我发现一个脚本工作,但它只是一个会话cookie。如何在此脚本中为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?

<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>


推荐答案

尝试:

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

c_value 是Cookie值


exdays is the number of days you want the cookie to expire after

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


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

这篇关于Cookie过期日期的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆