2017-04-17 90 views
0

如果我要在帶有JavaScript的用戶瀏覽器中測試localStorage,例如我可以通過測試localStorage推斷sessionStorage的可用性嗎?

var hasStorage = (function() { 
    try { 
     localStorage.setItem(mod, mod); 
     localStorage.removeItem(mod); 
     return true; 
    } catch (exception) { 
     return false; 
    } 
}()); 

if (hasStorage) { 
    // store things in places 
} 

(來源:https://mathiasbynens.be/notes/localstorage-pattern

我可以推斷(沒有明確的檢查),如果hasStorage === true,然後sessionStorage也可以,或者是有可能有一個殘疾人與其他支持?

+0

'localStorage'可以在鉻禁用。沒有選項可以禁用'sessionStorage'。如果有的話,推斷是如果'localStorage'被禁用,'sessionStorage'也將被禁用。這可以通過鉻啓動'鉻瀏覽器 - 禁用本地存儲'標誌,然後試圖設置'sessionStorage' – guest271314

+1

@blex在同一個兼容性表格中驗證,Firefox的支持順序相反。雖然有可能假定在正常操作中都存在,但沒有規範值得保證這種假設。所以,我想說,爲了安全起見,將它們作爲單獨的實體對待是很好的。 –

+0

有趣的是,使用'--disable-local-storage'標記集啓動了鉻,'sessionStorage'仍然被定義,試圖設置'localStorage'記錄'未捕獲的TypeError:無法讀取屬性'setItem'null' – guest271314

回答

1

localStorage與鉻不存在關聯sessionStorage。這是可能嘗試設置localStorage

Uncaught TypeError: Cannot read property 'setItem' of null 

但似乎沒有要在鉻List of Chromium Command Line Switches禁用sessionStorage官方標誌時--disable-local-storage標誌,它記錄錯誤發動鉻。請注意,鉻是使用鉻源代碼構建的。

+0

這很有趣。我可能會誤讀,但是你的研究也不會表明,如果'localStorage' **是**可用的(即沒有標記/禁用,如你所示),那麼'sessionStorage'也可用,因爲它不能儘可能從鏈接的規格中看出)被禁用,所以測試不需要重新運行兩者? – tymothytym

+0

'sessionStorage'可以禁用http://stackoverflow.com/questions/17882647/can-user-disable-html5-sessionstorage。建議檢查每個。 – guest271314

相關問題