cookie 的相关操作
// 设置cookie
function setCookie(name, value) {
var curDate = new Date();
//当前时间戳
var curTamp = curDate.getTime();
//当日凌晨的时间戳,减去一毫秒是为了防止后续得到的时间不会达到00:00:00的状态
var curWeeHours = new Date(curDate.toLocaleDateString()).getTime() - 1;
//当日已经过去的时间(毫秒)
var passedTamp = curTamp - curWeeHours;
//当日剩余时间
var leftTamp = 24 * 60 * 60 * 1000 - passedTamp;
var leftTime = new Date();
leftTime.setTime(leftTamp + curTamp);
document.cookie = name + "=" + escape(value) + ";expires=" + leftTime.toGMTString();
}
//删除cookie
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();
}
// 取出cookie
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg))
return unescape(arr[2]);
else
return null;
}