前端存、取、删cookie,设置cookie保留的时间

原创 15616397727 教程 JavaScrip 338阅读 2017-11-20 17:33:07 举报

function getCookie(name) {
try {
var arr, reg = new RegExp("(^| )" + name + "=([^;])(;|$)");
if (arr = document.cookie.match(reg)) {
// return unescape(arr[2]);
return decodeURIComponent(arr[2]);
} else {
return null;
}
} catch (e) { return null; }
}
function delCookie(name) {
var exp = new Date();
exp.setTime(exp.getTime() - 1);
var cval = getCookie(name);
if (cval != null)
document.cookie = name + "=" + cval + ";expires=" + exp.toGMTString() + ";path=/";
}
// s20是代表20秒
// h是指小时,如12小时则是:h12
// d是天数,30天则:d30
function setCookie(name, value, time) {
var strsec = getsec(time);
var exp = new Date();
exp.setTime(exp.getTime() + strsec
1);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString() + ";path=/";
}
function getsec(str) {
var str1 = str.substring(1, str.length) 1;
var str2 = str.substring(0, 1);
switch (str2) {
case "s": return str1
1000;
case "m": return str1 60 1000;
case "h": return str1 60 60 1000;
default: return str1
24 60 60 * 1000;
}
}

评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复