首页 课程 师资 教程 报名

在JavaScript中进行Cookie过期时间设置

  • 2022-06-20 09:26:54
  • 1955次 星辉

通过设置过期日期并将过期日期保存在 cookie 中,将 cookie 的生命周期延长到当前浏览器会话之外。这可以通过将 'expires' 属性设置为日期和时间来完成。

例子

您可以尝试运行以下示例将 cookie 的到期日期设置为 1 个月

<html>
   <head>
      <script>
         <!--
            function WriteCookie() {
               var now = new Date();
               now.setMonth( now.getMonth() + 1 );
               cookievalue = escape(document.myform.customer.value) + ";"
               document.cookie="name=" + cookievalue;
               document.cookie = "expires=" + now.toUTCString() + ";"
               document.write ("Setting Cookies : " + "name=" + cookievalue );
            }
         //-->
      </script>
   </head>
   <body>
      <form name="myform" action="">
         Enter name: <input type="text" name="customer"/>
         <input type="button" value="Set Cookie" onclick="WriteCookie()"/>
      </form>
   </body>
</html>

 

选你想看

你适合学Java吗?4大专业测评方法

代码逻辑 吸收能力 技术学习能力 综合素质

先测评确定适合在学习

在线申请免费测试名额
价值1998元实验班免费学
姓名
手机
提交