// 增加webStorage
sessionStorage.setItem('key', 'value');
localStorage.setItem('key', 'value');
sessionStorage.key = 'value';
localStorage.key = 'value';
// 删除webStorage
sessionStorage.removeItem('key');
localStorage.removeItem('key');
sessionStorage.clear();
localStorage.clear();
//修改webStorage
sessionStorage.setItem('key', 'newValue');
localStorage.setItem('key', 'newValue');
sessionStorage.key = 'newValue';
localStorage.key = 'newValue';
//查找webStorage
变量 = sessionStorage.getItem('key');
变量 = localStorage.getItem('key');
变量 = sessionStorage.key;
变量 = localStorage.key;
sessionStorage.key(index);
localStorage.key(index);
两个函数(set/get)
function setCookie(cname, cvalue, exdays) {
var d = new Date();
d.setTime(d.getTime() + (exdays * 24 * 60 * 60 * 1000));
var expires = "expires=" + d.toGMTString();
document.cookie = cname + "=" + cvalue + "; " + expires;
}
function getCookie(cname) {
var name = cname + "=";
var ca = document.cookie.split(';');
for (var i = 0; i < ca.length; i++) {
var c = ca[i].trim();
var arrC = c.split("=");
if (arrC[0] == cname) {
return arrC[1];
}
}
return "";
}
cookie插件
某些情况下为了快速使用cookie可以使用js插件帮助我们管理cookie的存取,例如:https://github.com/js-cookie/js-cookie