html5 localStorage ios8.1.1 safari兼容問題

引入:http://www.flyphp.cn/html5-localstorage-safari.htmlphp

 

移動端 不能使用localStorage 時,由於: 瀏覽器開啓了無痕模式。去~html

解決方式:html5

cookie瀏覽器

var localStorage = window.localStorage;
var prefix = 'local_storage_';
var local_ok = true;
try {
    localStorage.setItem('localStorageTest', '1');
    localStorage.removeItem('localStorageTest');
    //使用Storage
} catch (e) {
    //使用cookie替代實現
    local_ok = false;
}
var host = 'm.shop.letv.com';
var local_storage = {
    'setItem':function(key,val) {
        if (local_ok) {
            localStorage.setItem(key,val);
        } else {
            addCookie(prefix + key,val,host);undefined
        }
    },
    'getItem':function(key){
        if (local_ok) {
            return localStorage.getItem(key);
        } else {
            return getCookie(prefix + key);
        }
    },
    'removeItem':function(key){
        if (local_ok) {
            localStorage.removeItem(key);
        } else {
            delCookie(prefix + key);
        }
    },
    'clear':function(){
        if (local_ok) {
            localStorage.clear();
        } else {
            var aCookie = document.cookie.split(";");  
            var re = '';  
            for (var i = 0; i < aCookie.length; i++) {  
                var aCrumb = aCookie[i].split("=");
                if ($.trim(aCrumb[0]).indexOf(prefix) === 0) {
                    delCookie($.trim(aCrumb[0]));
                }
            }
        }
    }
}
相關文章
相關標籤/搜索