简介
jStorage是一个跨浏览器的将key-value类型的数据存储到浏览器本地存储的js插件——jStorage支持所有主流浏览器,PC机(甚至包括是IE6)和移动终端均可用。此外,jStorage的实现不依赖任何其它js库(库无关),它和其他js库(Query, Prototype, MooTools等)有良好的兼容性。但是为了让它能够支持老版本的IE(比如蛋疼的IE6),需要依赖第三方库(Prototype, MooTools)或JSON2。
jStorage支持存储Strings,Numbers,JavaScript对象,Arrays,甚至是原生XML节点。 jStorage还支持设置TTL(Time to Live, 生存时间)类型的值用于自动到期存储的keys。最重要的是(也是attractive的) - 当浏览器页签中某个key值变更时或者订阅/发布事件以通知其他标签/窗口的能力。这使得jStorage能够成为Web应用程序本地PubSub(publish/subscribe)平台。
jStorage非常小,min压缩后只有7kb,zip打包后只有4kb。
索引
- 基本信息
- 下载
- 交互测试
- 浏览器支持
- 用法
- 函数参考
- 使用示例
- 问题
- 联系和版权
基本信息
浏览器支持
Browser | Storage support | Survives browser restart | Survives browser crash | Storage size |
Chrome 4 | + | + | + | 5 MB |
Firefox 3.6 | + | + | + | 5 MB |
Firefox 3 | + | + | + | 5 MB |
Firefox 2 | + | + | + | 5 MB |
IE8 | + | + | + | 10 MB |
IE7 | + | + | + | 128 kB |
IE6 | + | + | + | 128 kB |
Opera 10.50 | + | + | - | 5 MB |
Opera 10.10 | - | N/A | N/A | N/A |
Safari 4 | + | + | + | 5 MB |
Iphone Safari | + | + | + | 5 MB |
Safari 3 | - | N/A | N/A | N/A |
- <script src="//cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
- <script src="https://raw.github.com/andris9/jStorage/master/jstorage.js"></script>
- <script> /* $.jStorage is now available */ </script>
- <script src="//cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
- <script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
- <script src="https://raw.github.com/andris9/jStorage/master/jstorage.js"></script>
- <script> /* $.jStorage is now available */ </script>
正确用法:$.jStorage.set("xml", xml_node)错误:$.jStorage.set("xml", {xml: xml_node})
- <script src="json2.js"></script>
- <script src="jquery.js"></script>
- <script src="jstorage.js"></script>
- <script>
- $.jStorage.set("key1", "value1", {TTL: 3000});
- setInterval(function(){
- var vaule = $.jStorage.get("key1");
- document.writeln(vaule + ";");
- },1000);
- </script>
get(key[, default])
value = $.jStorage.get(key)value = $.jStorage.get(key, "default value")
deleteKey(key)
$.jStorage.setTTL("mykey", 3000); // TTL3秒
对key设置TTL(毫秒数)。设置0或负值以删除key
getTTL(key)
ttl = $.jStorage.getTTL("mykey"); // TTL in milliseconds or 0获取key剩余的生存时间(毫秒数)。如果key值不存在或者未设置TTL,返回0
- <script>
- $.jStorage.set("key1", "value1", {TTL: 5000});
- setInterval(function(){
- var ttl = $.jStorage.getTTL("key1");
- document.writeln(ttl + ";");
- },1000);
- </script>
flush()
$.jStorage.flush() —— 清除所有keyindex()
$.jStorage.index() —— 以数组形式返回当前使用的所有keyvar index = $.jStorage.index();
console.log(index); // ["key1","key2","key3"]
storageSize()
$.jStorage.storageSize() —— 返回存储的数据所占用的bytescurrentBackend()
$.jStorage.currentBackend() —— 返回当前使用的存储引擎,如果没有存储引擎返回false。reInit()
$.jStorage.reInit() —— 重载数据。subscribe(channel, callback)
$.jStorage.subscribe("ch1", function(channel, payload){ // 订阅 PubSub通道console.log(payload+ " from " + channel);
});
- <script src="json2.js"></script>
- <script src="jquery.js"></script>
- <script src="jstorage.js"></script>
- <script>
- function sync(){
- $.jStorage.publish("sync-textarea", $("data").value);
- }
- $.jStorage.subscribe("sync-textarea", function(channel, data){
- $("data").value = data;
- });
- </script>
- <div>
- <h2>Interactive test - publish/subscribe</h2>
- <p>Open the same page in other tabs or windows of the same browser</p>
- <p><textarea id="data" style="width: 600px; height: 100px;"></textarea></p>
- <p><input type="button" id="nupp" value="Sync entered data with other tabs/windows" onclick="sync()"></p>
- </div>
listenKeyChange(key, callback)
$.jStorage.listenKeyChange("mykey", function(key, action){console.log(key + " has been " + action);
});
- <pre name="code" class="html"> <script src="json2.js"></script>
- <script src="jquery.js"></script>
- <script src="jstorage.js"></script>
- <script>
- function changeKey(){
- $.jStorage.set("mykey", 1);
- }
- function deleteKey(){
- $.jStorage.deleteKey("mykey");
- }
- $.jStorage.listenKeyChange("mykey", function(key, action){
- console.log(key + " has been " + action);
- });
- $.jStorage.listenKeyChange("mykey", function(key, action){
- console.log(key + " has been:::: " + action);
- });
- </script>
- <div>
- <p><input type="button" onclick="changeKey()" value="changeKey"></p>
- <p><input type="button" onclick="deleteKey()" value="deleteKey"></p>
- </div></pre>
stopListening(key[, callback])
$.jStorage.stopListening("mykey") —— 清除mykey的所有listeners,如果callback设置了,只有使用过的回调会被清除,否则所有的listeners都会被丢弃使用示例
- <span style="font-weight:normal">jQuery -jQuery没有内置JSON编码/解码器,如果要支持IE6//IE7,需要以如下方式引用
- <script src="//cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js"></script>
- <script src="//code.jquery.com/jquery-1.9.1.min.js"></script>
- <script src="jstorage.js"></script>
- <script>
- // Check if "key" exists in the storage
- var value = $.jStorage.get("key");
- if(!value){
- // if not - load the data from the server
- value = load_data_from_server()
- // and save it
- $.jStorage.set("key",value);
- }
- </script></span>
- <span style="font-weight:normal">Prototype
- <script src="//ajax.googleapis.com/ajax/libs/prototype/1.7.1.0/prototype.js"></script>
- <script src="jstorage.js"></script>
- <script>
- // Check if "key" exists in the storage
- var value = $.jStorage.get("key");
- if(!value){
- // if not - load the data from the server
- value = load_data_from_server()
- // and save it
- $.jStorage.set("key",value);
- }
- </script></span>
问题
- Cookies are not meant to save large quantities of data locally - they are meant to pass around ID values to keep track of users. Internet Explorer allows to use up to 20 cookies per domain with the payload of 4kB per cookie. It isn't very much, especially considering the need to chunk larger data into smaller bits. The fact that the data (max. 80 kB) is sent to the server with *every* call doesn't sound much of a good idea either.
联系和版权
jStorage is licensed under MIT-styled license , so basically you can do whatever you want to do with it.
参考: