lmislm / lmislm.github.io

hexo的配置
0 stars 0 forks source link

localStorage Tips #6

Open lmislm opened 11 months ago

lmislm commented 11 months ago
  1. 判断localstorage的时候,只有if(window.localstorage)可能不够,需要考虑隐私模式,Safari的隐私模式下,setItem和getItem会报错,所以进行操作的时候需要抛错处理。
  2. 判断getItem的值的时候,不能判断 != '',需要判断 != null, (规定的

    ... If the given key does not exist in the list associated with the object then this method must return null. ...

参考:

  1. 如何判断浏览器是否支持localstorage?
  2. 前端系列——window.localStorage空值判断问题
  3. https://stackoverflow.com/questions/3262605/how-to-check-whether-a-storage-item-is-set