Cookie封装
2020-01-11 本文已影响0人
燕自浩
老革命家Cookie
const cookies = {
cookie: {
// 设置cookie
set: (name, value, day) => {
const date = new Date();
date.setDate(date.getDate() + day);
document.cookie = name + "=" + value + ";expires=" + date;
},
// 获取cookie
get: (key) => {
var arr = document.cookie.split("; ");
for (var i = 0; i < arr.length; i++) {
var arr1 = arr[i].split("=");
if (arr1[0] == key) {
return arr1[1]
}
}
return ""
},
// 删除cookie
remove: (name) => {
cookie.set(name, 1, -10)
}
}
}
————————————————
版权声明:本文为CSDN博主「Vam的金豆之路」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。
原文链接:https://blog.csdn.net/qq_39045645/article/details/103903438
使用
cookie.set('maomin', '22', '0.5');
console.log(cookie.get("maomin"));