let cookie = {
/ / According to the key value to get the corresponding cookie
get:function(key){
//Get the cookie
let data = document.cookie;
/ / Get the first occurrence of the key position pwd=
let startIndex = data.indexOf(key+'=');
// name=123;pwd=abc
/ / If the starting index value is greater than 0 means there is a cookie
if(startIndex>-1) {
//The starting position of the key is equal to the position where it appears and the length of the key +1
startIndex = startIndex+key.length+1;
//The end position is equal to the position after the position starting from the key; the position where the number appears
let endIndex = data.indexOf(';',startIndex);
/ / If the end position is not found then the end position is equal to the length of the cookie, after the content is all obtained
endIndex = endIndex<0 ? data.length:endIndex;
return decodeURIComponent(data.substring(startIndex,endIndex));
}else {
return '';
}
},
set:function(key,value,time){
//Default save time
let time = time;
/ / Get the current time
let cur = new Date();
let undefined;
/ / Set the specified time
cur.setTime(cur.getTime()+time*24*3600*1000);
/ / Create a cookie and set the life cycle to GMT time
document.cookie = key+'='+encodeURIComponent(value)+';expires='+(time===undefined?'':cur.toGMTString());
},
del:function(key){
//Get the cookie
let data = this.get(key);
/ / If you get a cookie, reset the cookie life cycle to the past time
if(data!==false){
this.set(key,data,-1);
}
}
};
Copy code