[英]Can sessionStorage.getItem('key') trigger an exception? incognito mode? Any old mobile phone browser?
I'm curious if sessionStorage.getItem('mytestdata')
ever can trigger an exception. 我很好奇
sessionStorage.getItem('mytestdata')
是否会触发异常。 I'm NOT talking about 我不是在说
localStorage.setItem('key', 'value')
but rather about 而是关于
sessionStorage.getItem('key')
Thank you. 谢谢。
Yes, but just if you are using a very old version of web browser, not if a key doesn't exists. 是的,但是就算您使用的是旧版的网络浏览器,也不存在不存在的密钥。 You can check compatibility here https://developer.mozilla.org/es/docs/Web/API/Window/sessionStorage
您可以在此处检查兼容性https://developer.mozilla.org/es/docs/Web/API/Window/sessionStorage
If any item not set in localstorage
then you need to check it by typeof != 'undefined'
如果未在
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.