欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

localStorage的使用方法以及一些注意事项

程序员文章站 2024-02-28 08:46:22
...

基本使用方法:

[Storage.key()
](https://developer.mozilla.org/en-US/docs/Web/API/Storage/key)
      //When passed a number n, this method will return the name of the nth key in the storage.

[Storage.getItem()
](https://developer.mozilla.org/en-US/docs/Web/API/Storage/getItem)
      //When passed a key name, will return that key's value.
[Storage.setItem()
](https://developer.mozilla.org/en-US/docs/Web/API/Storage/setItem)
      //When passed a key name and value, will add that key to the storage, or update that key's value if it already exists.
[Storage.removeItem()
](https://developer.mozilla.org/en-US/docs/Web/API/Storage/removeItem)
      //When passed a key name, will remove that key from the storage.
[Storage.clear()
](https://developer.mozilla.org/en-US/docs/Web/API/Storage/clear)
   //When invoked, will empty all keys out of the storage.

其他:

  • 只能存储字符串
  • chrome 56下, 极限大小是5M左右.
  • Safari无痕模式下设置localStorage会抛出异常.