js cookie操作
2019-07-15 本文已影响0人
前端小旋风
存cookie
function setCookie(name, value) {
const Days = 30000;
let exp = new Date();
exp.setTime(exp.getTime() + Days * 24 * 60 * 60 * 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
取cookie
function getCookie(name) {
let arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg)) {
return unescape(arr[2]);
} else { return null; }
}
清除cookie
function clearCookie() {
var date = new Date();
date.setTime(date.getTime() - 10000);
var keys = document.cookie.match(/[^ =;]+(?=\=)/g);
if (keys) {
for (var i = keys.length; i--;)
document.cookie = keys[i] + "=0; expire=" + date.toGMTString() + "; path=/";
}
}
我建了一个前端微信交流群,欢迎大家加入,qq中转群号:1076484243