引入:http://www.flyphp.cn/html5-localstorage-safari.html
移动端 不能使用localStorage 时,因为: 浏览器开启了无痕模式。去~
解决方式:
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])); } } } } }