产品设计

Web Storage全解析

此外关于setItem(key,value)方法中的value类型,理论上可以是任意类型,不过实际上浏览器会调用value的toString方法来获取其字符串值并存储到本地,因此如果是自定义的类型则需要自己定义有意义的toString方法。

事件

标准的事件为onstorage,当存储空间中的数据发生变化时触发。此外,IE8中新增了一个onstoragecommit事件,当数据写入的时候触发。onstorage事件中的事件对象应该支持以下属性:

The key attribute represents the key being changed.
The oldValue attribute represents the old value of the key being changed.
The newValue attribute represents the new value of the key being changed.
The url attribute represents the address of the document whose key changed.
The storageArea attribute represents the Storage object that was affected.

对于这一标准的实现,webkit内核的浏览器(Chrome、Safari)以及Opera是完全遵循标准的,IE8则只实现了url,Firefox下则均未实现。

测试地址为:http://varnow.org/pages/html5/web_storage/local/event.html

具体结果见表3。

表3 onStorage事件对象属性测试

  key oldValue newValue url storageArea
IE8
Firefox3.6
Chrome5
Safari4
Opera10
希望看到您的想法,请您发表评论x