瀏覽器本地儲存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的支持

相關文章
相關標籤/搜索