更新時(shí)間:2022-06-20 09:26:54 來源:動力節(jié)點(diǎn) 瀏覽2384次
通過設(shè)置過期日期并將過期日期保存在 cookie 中,將 cookie 的生命周期延長到當(dāng)前瀏覽器會話之外。這可以通過將 'expires' 屬性設(shè)置為日期和時(shí)間來完成。
您可以嘗試運(yùn)行以下示例將 cookie 的到期日期設(shè)置為 1 個(gè)月
<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>
初級 202925
初級 203221
初級 202629
初級 203743