Note: 對於永久cookie我們用了Fri, 31 Dec 9999 23:59:59 GMT
作為過期日。如果你不想使用這個日期,可使用世界末日Tue, 19 Jan 2038 03:14:07 GMT,
它是32位帶符號整數能表示從1 January 1970 00:00:00 UTC開始的最大秒長(即01111111111111111111111111111111
, 是 new Date(0x7fffffff * 1e3)
).
Infinity 過期時間 9999-12-31 是 9999年12月31日;
對應 js 操作
<script> //cookie function addCookie(objName,objValue,objDays){ var str = objName + "=" + escape(objValue); console.log(Infinity); //Infinity console.log(typeof Infinity); //number console.log(Infinity.constructor); //function Number() { [native code] } if(objDays > 0){ var date = new Date(); var ms = objDays*24*3600*1000; date.setTime(date.getTime() + ms); str += "; expires=" + date.toGMTString(); } if(objDays===Infinity){ str += "; expires=Fri, 31 Dec 9999 23:59:59 GMT"; } str += "; path=/"; document.cookie = str; }; var itemtitle=document.querySelector(".item-title"); document.querySelector(".sava_cookie").addEventListener("click",function(e){ var putCookieVal=itemtitle.innerHTML; addCookie('surface',putCookieVal,Infinity); }); </script>
我們設置3個cookie ,看下瀏覽器的記錄;
上面三條cookie記錄 現在是2016年6月
seseion:cookieMaxAge 過期時間 瀏覽器會話期間
hunred-day:cookieMaxAge 過期時間2016年9月11日 固定的一個時間
surface:cookieMaxAge 過期時間9999年12月31日 最大值
addCookie('surface','cookieMaxAge',Infinity);
addCookie('hunred-day','cookieMaxAge',100);
addCookie('Session','cookieMaxAge');
第三條 如果不設置expires或者max-age這個cookie默認是Session的,也就是關閉瀏覽器該cookie就消失了。
備注:
Note: 對於永久cookie我們用了Fri, 31 Dec 9999 23:59:59 GMT
作為過期日。如果你不想使用這個日期,可使用世界末日Tue, 19 Jan 2038 03:14:07 GMT,
它是32位帶符號整數能表示從1 January 1970 00:00:00 UTC開始的最大秒長(即01111111111111111111111111111111
, 是 new Date(0x7fffffff * 1e3)
).
參開網站
https://developer.mozilla.org/zh-CN/docs/Web/API/Document/cookie