js封裝cookie的操作
阿新 • • 發佈:2019-02-02
var cookie = {
/**
* 設定cookie
* @param key cookie名
* @param value cookie值
* @param expires 失效時間
* @param path 路徑
* @param domain 域名
* @param isSecure 是否進行安全設定
*/
setCookie : function(key,value,expires,path,domain,isSecure){
//獲取多個cookie值用&連線的,或只是獲取name的value值
var str=encodeURIComponent(key)+"="+encodeURIComponent(value);
//獲取一個cookie值
//var str=key+"="+value;
if(typeof expires == "number"){
var date = new Date();
date.setDate(date.getDate() + expires);
str+=";expires="+date;
}
if(path){
str+=";path="+path;
}
if(domain){
str+=";domain="+domain;
}
if(isSecure){
str+=";secure";
}
document.cookie= str;
},
//清除cookie
removeCookie : function(key) {
//此時this代表cookie物件
this.setCookie(key,"",-1);
},
//獲取cookie的value值
getCookie: function(key){
var str = document.cookie;
var arr = str.split("; ");
for(var i = 0;i < arr.length;i++){
if(arr[i].indexOf(key) != -1){
var temp = arr[i].split("=");
if(temp[0] == key){
return decodeURIComponent(temp[1]);
}
}
}
return null;
},
//獲取cookie的value值
getCookieFromMulti : function(key1,key2){
var strValue = cookie.getCookie(key1);
if(!strValue){
return null;
}
var arrValue = strValue.split("&");
if(!key2){
return arrValue[0];
}
for(var i = 0;i < arrValue.length;i++){
if(arrValue[i].indexOf(key2) != -1){
var tempValue = arrValue[i].split("=");
if(tempValue[0] == key2){
return decodeURIComponent(tempValue[1]);
}
}
}
return null;
},
//改變cookie中任意變數的值
setCookieAtMulti : function(key1,key2,value){
var vals = this.getCookie(key1);
var arrValList = vals.split("&");
for(var i = 0; i < arrValList.length; i++){
if(arrValList[i].indexOf(key2) != -1){
var temp = arrValList[i].split("=");
if(temp[0] == key2){
arrValList[i] = key2 + "=" + value;
var vals = arrValList.join("&");
this.setCookie(key1,vals);
break;
}
}
}
}
};
/**
* 設定cookie
* @param key cookie名
* @param value cookie值
* @param expires 失效時間
* @param path 路徑
* @param domain 域名
* @param isSecure 是否進行安全設定
*/
setCookie : function(key,value,expires,path,domain,isSecure){
//獲取多個cookie值用&連線的,或只是獲取name的value值
var str=encodeURIComponent(key)+"="+encodeURIComponent(value);
//獲取一個cookie值
//var str=key+"="+value;
if(typeof expires == "number"){
var date = new Date();
date.setDate(date.getDate() + expires);
str+=";expires="+date;
}
if(path){
str+=";path="+path;
}
if(domain){
str+=";domain="+domain;
}
if(isSecure){
str+=";secure";
}
document.cookie= str;
},
//清除cookie
removeCookie : function(key) {
//此時this代表cookie物件
this.setCookie(key,"",-1);
},
//獲取cookie的value值
getCookie: function(key){
var str = document.cookie;
var arr = str.split("; ");
for(var i = 0;i < arr.length;i++){
if(arr[i].indexOf(key) != -1){
var temp = arr[i].split("=");
if(temp[0] == key){
return decodeURIComponent(temp[1]);
}
}
}
return null;
},
//獲取cookie的value值
getCookieFromMulti : function(key1,key2){
var strValue = cookie.getCookie(key1);
if(!strValue){
return null;
}
var arrValue = strValue.split("&");
if(!key2){
return arrValue[0];
}
for(var i = 0;i < arrValue.length;i++){
if(arrValue[i].indexOf(key2) != -1){
var tempValue = arrValue[i].split("=");
if(tempValue[0] == key2){
return decodeURIComponent(tempValue[1]);
}
}
}
return null;
},
//改變cookie中任意變數的值
setCookieAtMulti : function(key1,key2,value){
var vals = this.getCookie(key1);
var arrValList = vals.split("&");
for(var i = 0; i < arrValList.length; i++){
if(arrValList[i].indexOf(key2) != -1){
var temp = arrValList[i].split("=");
if(temp[0] == key2){
arrValList[i] = key2 + "=" + value;
var vals = arrValList.join("&");
this.setCookie(key1,vals);
break;
}
}
}
}
};