HTML5 sessionStorage 使用

HTML5 sessionStorage 使用

实例:

function restore4Local() {
    if (window.localStorage) {
        user = user || {};
        window.sessionId = localStorage.getItem("sessionId");
        if (window.sessionId == null || window.sessionId == undefined) {
            return;
        }
        user.username = localStorage.getItem("user_username");
        user.password = localStorage.getItem("user_password");
        user.userId = localStorage.getItem("user_userId");
    }
}
var restoreLoginDialog = function () {
    var $username = $('#uib_login input[name=username]');
    var $password = $('#uib_login input[name=password]');
    if (window.localStorage) {
        if (!$username.val()) {
            $username.val(localStorage.getItem("user_username"));
            $password.val(localStorage.getItem("user_password"));
        }
    }
};
/***
 * 清除localStorage
 */
function removeLocalStorage() {
    localStorage.removeItem("sessionId");
    localStorage.removeItem("user_username");
    localStorage.removeItem("user_password");
    localStorage.removeItem("user_userId");
}
/***
 * 使用sessionStorage保存数据
 * @param key
 * @param objectVal : json对象
 */
export function saveObj(key,objectVal) {
    window.sessionStorage.setItem(key, JSON.stringify(objectVal));
};
/***
 * 使用sessionStorage恢复数据
 * @param key
 */
export function getObj(key) {
    var val=window.sessionStorage.getItem(key);
    if(val){
        return JSON.parse(val);
    }else{
        return val;
    }
};
export function removeItem(key) {
    window.sessionStorage.removeItem(key);
};
export function removeStoreCache() {
    window.sessionStorage.removeItem(SESSIONSTORAGE_KEY_GZQ_APPLIST);
    window.sessionStorage.removeItem(SESSIONSTORAGE_KEY_GZQ_MOBILESTORECONFIG);
};

参考:

https://developer.mozilla.org/zh-CN/docs/Web/API/Web_Storage_API/Using_the_Web_Storage_API

https://my.oschina.net/crazymus/blog/371757

相关推荐