[英]Can sessionStorage.getItem('key') trigger an exception? incognito mode? Any old mobile phone browser?
我很好奇sessionStorage.getItem('mytestdata')
是否會觸發異常。 我不是在說
localStorage.setItem('key', 'value')
而是關於
sessionStorage.getItem('key')
謝謝。
是的,但是就算您使用的是舊版的網絡瀏覽器,也不存在不存在的密鑰。 您可以在此處檢查兼容性https://developer.mozilla.org/es/docs/Web/API/Window/sessionStorage
如果未在localstorage
設置任何項目,則需要通過typeof != 'undefined'
進行檢查
if(typeof sessionStorage.getItem('key') !== null){
//The `key` is set in the `localStorage`
}
if(typeof sessionStorage.getItem('key') != 'undefined'){
//The `key` and `value` both are set in the `localStorage`
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.