一、localStorage
localStorage 用于长久保存整个网站的数据,保存的数据没有过期时间,直到手动去删除。
localStorage 属性是只读的。
如:localStorage.setItem(“lastname”, “Smith”);
二、sessionStorage
如果你只想将数据保存在当前会话中,可以使用 sessionStorage 属性, 该数据对象临时保存同一窗口(或标签页)的数据,在关闭窗口或标签页之后将会删除这些数据。
如:window.sessionStorage.setItem(“cTime”, this.cTime);
三、cookie
1、cookie自定义存储天数,如7天
setCheckedCookie(c_name, c_pwd,exdays) {
//记住密码状态时的设置cookie,参数分别为用户名,密码,用户级别,图幅权限,保存天数
var exdate = new Date(); //获取时间
exdate.setTime(exdate.getTime() + 24 * 60 * 60 * 1000 * exdays); //保存的天数
//字符串拼接cookie
window.document.cookie =
"userName" + "=" + c_name + ";path=/;expires=" + exdate.toGMTString();
window.document.cookie =
"password" + "=" + c_pwd + ";path=/;expires=" + exdate.toGMTString();
},
2、关闭浏览器本身,cookie清除
setCookie(c_name,c_password) {
//字符串拼接cookie
window.document.cookie =
"userName" + "=" + c_name
window.document.cookie =
"password" + "=" + c_password
}
附: