简体   繁体   中英

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. 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

If any item not set in localstorage then you need to check it by 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`    
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM