如何设置Cookie在JavaScript中在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>热门推荐
10 写给情侣祝福语 浪漫简短
11 师姐毕业送花祝福语简短
12 简短霸气的考试祝福语
13 俄语祝福语贺卡文案简短
14 生日祝福语贺卡文字简短
15 周日徒步祝福语大全简短
16 手写母亲的祝福语简短
17 喜事敬酒祝福语简短精辟
18 长辈见面花束祝福语简短