JS操作Cookie
var Cookies = {
//读取Cookie, n为Cookie名
Get : function(n){
var re = new RegExp(n +'=([^;]*);?','gi');
var r =re.exec(document.cookie)||[];
return (r.length> 1 ? r[1] : null)
},
//读取数组Cookie,cookieName为Cookie名, keyName为要获取的键
GetArray :function(cookieName, keyName){
var oRegex1 = newRegExp(cookieName + '=([^;]+)','i');
var values1 =oRegex1.exec(document.cookie);
if(values1&& values1.length >1){
var values2 =values1[1]
var oRegex2 =new RegExp(keyName + '=([^&]+)','i');
var oMatch =oRegex2.exec(values2);
if(oMatch&& oMatch.length >1)
return(oMatch[1]);
else
return'';
}
else
return'';
},
//写入Cookies(不可写数组), n为Cookie名,v为Cookie值, h为时间(单位为小时), p为Cookie路径Path, d为Domain, s为secure
Set:function(n,v,h,p,d,s){
var t = new Date();
if(h){
// 8.64e7 一天3.6e6 一小时
t.setTime(t.getTime()+ (h*3.6e6));
}
document.cookie = n + '=' +escape(v) + '; ' + (!h ? '' : '; expires=' + t.toGMTString()) + (!p? '' : '; path=' + p) + (!d ? '' : '; domain=' + d) + (!s ? '' : ';secure') // Set cookie
},
//删除Cookie, n为Cookie名,p为Cookie路径Path, d为Domain
Del:function(n,p,d){
var t = newDate();//获取当前时间
t.setTime(t.getTime() -10000);//将date设置为过去的时间
document.cookie = n + '=' + (!p? '' : '; path=' + p) + (!d ? '' : '; domain=' + d) + '; expires='+ t.toGMTString();
}
};
摘自 wjbaiverson的专栏
补充:web前端 , JavaScript ,