Cookie

JS:

补习一下 js Date类知识: 

1 //获取两分钟后的时间,并以当前时区输出:
2 var d=new Date();
3 d.setTime(d.getTime()+1000*120);
4 document.write(d.toUTCString());
5 
6 //获取40天后的时间,并输出:
7 var d=new Date();
8 d.setDate(d.getDate()+40)
9 document.write(d.toUTCString());
View Code

设置Cookie:

只存活30秒的cookie: 

var d=new Date();
d.setTime(d.getTime()+1000*30);
document.cookie="xxx='1234'"+";expires="+d.toUTCString();

只存活40天的cookie:

var d=new Date();
d.setTime(d.getDate()+40);
document.cookie="xxx='1234'"+";expires="+d.toUTCString();

拓展:

PHP中的Cookies和Js中的Cookies是指的是同一个文件中的内容,PHP中设置的 expires参数为一个时间段,指定存活时间有多少秒。JS则直接指定一个时间,指定存活时间到什么为止。

  

原文地址:https://www.cnblogs.com/canbefree/p/3723375.html