浏览器本地储存jStorage

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

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

使用方法很简单

  1. Function reference

set(key, value[, options])

$.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.

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.

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.

deleteKey(key)

$.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的支持

转载于:https://my.oschina.net/zhouyuan/blog/117488

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值