浏览器本地储存jStorage

目前在开发一个即时通信的项目,对于一些聊天记录,但愿能够本地储存, 这样不须要再走服务器, 发现 一个Jquery 的插件支持这个功能,还支持IE6~

https://github.com/andris9/jStorage 浏览器支持一览node

使用方法很简单git

  1. Function reference

set(key, value[, options])github

$.jStorage.set(key, value, options) Saves a value to local storage. key needs to be string otherwise an exception is thrown. value can be any JSONeable value, including objects and arrays or a XML node. Currently XML nodes can't be nested inside other objects: $.jStorage.set("xml", xml_node) is OK but $.jStorage.set("xml", {xml: xml_node}) is not.json

Options is an optional options object. Currently only available option is options.TTL which can be used to set the TTL value to the key ($.jStorage.set(key, value, {TTL: 1000});). NB - if no TTL option value has been set, any currently used TTL value for the key will be removed.windows

get(key[, default])浏览器

value = $.jStorage.get(key) value = $.jStorage.get(key, "default value")服务器

get retrieves the value if key exists, or default if it doesn't. key needs to be string otherwise an exception is thrown. default can be any value.ide

deleteKey(key)this

$.jStorage.deleteKey(key) Removes a key from the storage. key needs to be string otherwise an exception is thrown.插件

setTTL(key, ttl)

$.jStorage.set("mykey", "keyvalue"); $.jStorage.setTTL("mykey", 3000); // expires in 3 seconds Sets a TTL (in milliseconds) for an existing key. Use 0 or negative value to clear TTL.

getTTL(key)

ttl = $.jStorage.getTTL("mykey"); // TTL in milliseconds or 0 Gets remaining TTL (in milliseconds) for a key or 0 if not TTL has been set.

flush()

$.jStorage.flush() Clears the cache.

index()

$.jStorage.index() Returns all the keys currently in use as an array. var index = $.jStorage.index(); console.log(index); // ["key1","key2","key3"]

storageSize()

$.jStorage.storageSize() Returns the size of the stored data in bytes

currentBackend()

$.jStorage.currentBackend() Returns the storage engine currently in use or false if none

reInit()

$.jStorage.reInit() Reloads the data from browser storage

storageAvailable()

$.jStorage.storageAvailable() Returns true if storage is available

subscribe(channel, callback)

$.jStorage.subscribe("ch1", function(channel, payload){ console.log(payload+ " from " + channel); }); Subscribes to a Publish/Subscribe channel (see demo)

publish(channel, payload)

$.jStorage.publish("ch1", "data"); Publishes payload to a Publish/Subscribe channel (see demo)

listenKeyChange(key, callback)

$.jStorage.listenKeyChange("mykey", function(key, action){ console.log(key + " has been " + action); }); Listens for updates for selected key. NB! even updates made in other windows/tabs are reflected, so this feature can also be used for some kind of publish/subscribe service.

stopListening(key[, callback])

$.jStorage.stopListening("mykey"); // cancel all listeners for "mykey" change Stops listening for key change. If callback is set, only the used callback will be cleared, otherwise all listeners will be dropped.

竟然还有频道订阅和发布的功能, 比较强大

PS IE6下须要json2.js的支持

相关文章
相关标签/搜索