js cookie,sessionStorage,localStorage 操作

sessionStorage

// 设置
sessionStorage.setItem(name, val);
// 获取
sessionStorage.getItem(name);
// 删除
sessionStorage.removeItem(name);
// 清空
sessionStorage.clear();

localStorage

// 设置
localStorage.setItem(name, val);
// 获取
localStorage.getItem(name);
// 删除
localStorage.removeItem(name);
// 清空
localStorage.clear();

cookie

// 设置cookie
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;
}

// 获取cookie
// 方法一:
function getCookie(cname){
    var name = cname + "=";
    var ca = document.cookie.split(';');
    for(var i=0; i

个人博客

你可能感兴趣的:(js cookie,sessionStorage,localStorage 操作)