专注Java教育14年 全国咨询/投诉热线:400-8080-105
动力节点LOGO图
始于2009,口口相传的Java黄埔军校
首页 hot资讯 在JavaScript中进行Cookie过期时间设置

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

更新时间:2022-06-20 09:26:54 来源:动力节点 浏览1932次

通过设置过期日期并将过期日期保存在 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>

 

提交申请后,顾问老师会电话与您沟通安排学习

免费课程推荐 >>
技术文档推荐 >>