//设置cookie
function setCookie(attr,value,time){
if(time){
var newtime=new Date();
newtime.setTime(newtime.getTime()+time*1000);
document.cookie=attr+"="+value+";expires="+newtime.toGMTString(); }else{
document.cookie=attr+"="+value;
}
}
//获取cookie
function getCookie(attr){
var cookies=document.cookie;
var arr=cookies.split("; ");
for (var i=0;i<arr.length;i++) {
var brr=arr[i].split("=");
if(brr[0]==attr){
return brr[1];
}
}
return false;
}
//删除cookie
function del(attr){
var newtime=new Date();
newtime.setTime(newtime.getTime()-1);
document.cookie=attr+"=lue;expires="+newtime.toGMTString();
}
cookie注意
只有大小 4k字节 4096字节,就是存储在计算机中的一些字符串, 结构就是一种键值对的形式,不能跨浏览器访问, 按域名存储,两种类型的cookie,临时的cookie,指定生命周期的cookie
cookie的设置
document.cookie="zhanghao=lisi";
document.cookie="pass=123";
var newobj=new Date();
newobj.setTime(newobj.getTime()+40*1000);
document.cookie="zhanghao=lisi;expires="+newobj.toGMTString();
console.log(document.cookie);
setCookie("zhangsan","lisi");
setCookie("zhanan","ji");
delCookie("zhanan")
console.log(document.cookie);