如何在 JavaScript 中设置在 30 分钟到期的 cookie?
可以通过设置过期日期并将过期日期保存在 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