如何在 JavaScript 中设置 Cookie 失效日期?
通过设置有效日期并在 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
iOS
HTML
CSS
安卓
Python
C 编程
C++
C#
MongoDB
MySQL
JavaScript
PHP