Cookie的创建与清除
// 存储Cookie
setCookie (name, value) {
if (value) {
var days = 1; //定义一天
var exp = new Date();
exp.setTime(exp.getTime() + days * 24 * 60 * 60 * 1000);
// 写入Cookie, toGMTString将时间转换成字符串
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString;
}
},
// 清除所有的Cookie
clearCookie(){
var keys=document.cookie.match(/[^ =;]+(?=\=)/g);
console.log(keys);
if (keys) {
for (var i = keys.length; i--;)
document.cookie=keys[i]+'=0;expires=' + new Date( 0).toUTCString()
}
},
本文标题:Cookie的创建与清除
本文链接:https://www.haomeiwen.com/subject/ohxscktx.html
网友评论