如何在 JavaScript 中设置 Cookie 在 30 分钟后过期?
通过设定过期时间并在 cookie 中保存过期时间,你可以延长 cookie 超出当前浏览器会话的生命周期。这可以通过将“expires”属性设定为某个日期和时间来实现。
示例
你可以尝试运行以下示例以设置 cookie 在 30 分钟后过期
<html>
<head>
<script>
<!--
function WriteCookie() {
var now = new Date();
var minutes = 30;
now.setTime(now.getTime() + (minutes * 60 * 1000));
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>
广告
数据结构
网络
RDBMS
操作系统
Java
iOS
HTML
CSS
Android
Python
C 编程
C++
C#
MongoDB
MySQL
Javascript
PHP