因爲Safari的隱身模式下本地存儲會被禁用,若是你嘗試往localStorage寫數據的話,會報超出使用限制的錯誤: QuotaExceededError (DOM Exception 22): The quota has been exceeded.
cookie
Data.hasLocalStorage = true; try{ window.localStorage.trySetData = 1; }catch(e){ Data.hasLocalStorage = false; } setLocalData: function(key, value){ if(Data.hasLocalStorage){ window.localStorage[key] = value; } else{ util.setCookie("_LOCAL_DATA_" + key, value, 1000); } }, getLocalData: function(key){ if(Data.hasLocalStorage){ return window.localStorage[key]; } else{ return util.getCookie("_LOCAL_DATA_" + key); } }
上面代碼作了個兼容,若是不支持localStorage就使用cookie。要注意cookie一個域名最多隻能有4kB,50個key,而本地存儲限制爲5Mb.code