JavaScript cookie中的expires选项是可选的。“ expires”属性是可选的。如果为该属性提供有效的日期或时间,则cookie将在给定的日期或时间到期,此后,将无法访问cookie的值。
要设置没有有效期的JavaScript cookie,请尝试运行以下代码-
<html>
<head>
<script>
<!--
function ReadCookie() {
var allcookies = document.cookie;
document.write ("All Cookies : " + allcookies );
//获取数组中的所有cookie对
cookiearray = allcookies.split(';');
//现在从该数组中取出键值对
for(var i=0; i<cookiearray.length; i++) {
name = cookiearray[i].split('=')[0];
value = cookiearray[i].split('=')[1];
document.write ("Key is : " + name + " and Value is : " + value);
}
}
//-->
</script>
</head>
<body>
<form name="myform" action="">
<p> click the following button and see the result:</p>
<input type="button" value="Get Cookie" onclick="ReadCookie()"/>
</form>
</body>
</html>