欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

js cookie

程序员文章站 2022-07-13 12:29:30
...


//存cookies
function SetCookie(name,value){
var Days = 30; //30 天
var exp = new Date(); //new Date("December 31, 9998");
exp.setTime(exp.getTime() + Days*24*60*60*1000);
document.cookie = name + "="+ escape (value) + ";expires=" + exp.toGMTString();
}
//取cookies
function getCookie(name) {
var arr = document.cookie.match(new RegExp("(^| )"+name+"=([^;]*)(;|$)"));
if(arr != null) return unescape(arr[2]); return null;
}
//删除cookie
function delCookie(name){

var cookarr=document.cookie.toString().split(";");
var date=new Date()
var timeout=parseFloat(date.getTime())-(30*24*3600*1000);
for (var i=0;i<cookarr.length;i++){
var cookarrsub=cookarr[i].split("=");
if (name == cookarrsub[0]){
//document.cookie=cookarrsub[0]+"="+cookarrsub[1]+";expires="+timeout.toString();
document.cookie=cookarrsub[0]+"=;expires="+timeout.toString();
}
}
}