如何在 JavaScript 中设置 cookie 的到期日期?
javascriptweb developmentfront end technology
通过设置到期日期并将到期日期保存在 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>
相关文章
有用资源
javascript 参考教程 - 该教程包含有关 javascript 的更多信息:https://www.cainiaomax.com/javascript/

