cookie/localStorage/sessionStorage之间的区别

原创 827538259 随笔 H5 103阅读 2018-01-08 17:09:28 举报

需求:刷新页面时保存当前页面所填入的数据
//设置缓存
function setCookie(name, value) {
var Days = 30;
var exp = new Date();
exp.setTime(exp.getTime() + Days 24 60 60 1000);
document.cookie = name + "=" + escape(value) + ";expires=" + exp.toGMTString();
}
//读取缓存
function getCookie(name) {
var arr, reg = new RegExp("(^| )" + name + "=([^;]*)(;|$)");
if (arr = document.cookie.match(reg))
return unescape(arr[2]);
else
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();
}

cookie/localStorage/sessionStorage之间的区别

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

赶紧努力消灭 0 回复