Please note, this is a STATIC archive of website developer.mozilla.org from November 2016, cach3.com does not collect or store any user information, there is no "phishing" involved.

Window.localStorage

localStorage 属性允许你访问一个 local Storage 对象。localStorage 与 sessionStorage 相似。不同之处在于,存储在 localStorage 里面的数据没有过期时间(expiration time),而存储在 sessionStorage 里面的数据会在浏览器会话(browsing session)结束时被清除,即浏览器关闭时。

语法

myStorage = localStorage;

返回值

一个 Storage 对象。

示例

下面的代码访问当前域名下的 local Storage 对象,并使用 Storage.setItem() 方法往里面添加一个数据项。

localStorage.setItem('myCat', 'Tom');

备注: 完整的使用示例可以查看使用 Web Storage API一文。

规范

Specification Status Comment
Web Storage (Second edition)
localStorage
Recommendation  

浏览器兼容性

Feature Chrome Firefox (Gecko) Internet Explorer Opera Safari (WebKit)
localStorage 4 3.5 8 10.50 4
sessionStorage 5 2 8 10.50 4
Feature Android Firefox Mobile (Gecko) IE Phone Opera Mobile Safari Mobile
Basic support 2.1 ? 8 11 iOS 3.2

各浏览器支持的 localStorage 和 sessionStorage 容量上限不同。测试页面 detailed rundown of all the storage capacities for various browsers

Note: since iOS 5.1, Safari Mobile stores localStorage data in the cache folder, which is subject to occasional clean up, at the behest of the OS, typically if space is short.

相关链接

文档标签和贡献者

 此页面的贡献者: AlexChao
 最后编辑者: AlexChao,