1.设置Cookie
function setCookie(name,value,iday){
var day = new Date();
day = setDate(day.getDate()+iday);
document.cookie=name+"="+value+";expires"+day;
}
2.获取指定name的Cookie
function getCookie(name) {
var strCookie=document.cookie;
var arrCookie=strCookie.split(";");
for(var i=0;i<arrCookie.length;i++){
if (arrCookie[i].indexOf(" ") == 0) {
arrCookie[i] = arrCookie[i].substring(1);
}
var c=arrCookie[0].split("=");
if(c[0]==name){
return c[1];
}
}
return "";
}
3.删除Cookie
function delCookie(name) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = getCookie(name);
if (cval != null) {
// 这里删除操作其实是将expires过期时间设置为当前时间,使cookie立即过期
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString();
}
}
网友评论