jStorage is a simple wrapper plugin for Prototype, MooTools and jQuery to cache data (string, numbers, objects, even XML nodes) on browser side. Note that functions, DOM nodes, host objects and such can not be saved.
jStorage was first developed under the name of DOMCached but since a lot of features were dropped to make it simpler (like the support for namespaces and such) it was renamed. DOMCached had separate files for working with Prototype and jQuery but jStorage can handle both in one go.
jStorage makes use of HTML5 local storage where available and userData behavior in Internet Explorer older versions. Webkit SQLite is not supported.
Current availability: jStorage supports all major browsers - Internet Explorer 6+, Firefox 2+, Safari 4+, Chrome 4+, Opera 10.50+
If the browser doesn't support data caching, then no exceptions are raised - jStorage can still be used by the script but nothing is actually stored.
jStorage is really small, just about 2 kB when minified (under 1kB when gzipped)!
jStorage can be downloaded at github (direct download link)
Add some values and refresh the page - if your browser supports storing local data, then the values should survive the page refresh.
| KEY | VALUE | |
| ADD | ||
| Clicking "GET" alerts the value for provided key with the method $.jStorage.get(key) | GET | |
| Clear all saved data | FLUSH |
Test this functionality with a specific library
| 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 |
jStorage requires Prototype, MooTools or jQuery + jQuery-JSON. Either way, the syntax stays the same.
$.jStorage.set(key, value)
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.
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.
$.jStorage.deleteKey(key)
Removes a key from the storage. key needs to be string otherwise an exception is thrown.
$.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.
$.jStorage.flush()
Clears the cache.
$.jStorage.index()
Returns all the keys currently in use as an array.
var index = $.jStorage.index();
console.log(index); // ["key1","key2","key3"]
$.jStorage.storageSize()
Returns the size of the stored data in bytes
$.jStorage.currentBackend()
Returns the storage engine currently in use or false if none
$.jStorage.reInit()
Reloads the data from browser storage (for example if you have reasons to believe that the data has been altered in another tab etc.)
$.jStorage.storageAvailable()
Returns true if storage is available
This example uses Prototype library
<script src="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>
jStorage is written in plain JavaScript and it requires a library (jQuery, Prototype, MooTools) for JSON encoding/decoding purposes only. If you have your own JSON implementation then you might want to skip the usage of an external library altogether.
<script>
$ = {
toJSON : your_json_encoder,
evalJSON : your_json_decoder
}
</script>
<script src="jstorage.js"></script>
<script>alert($.jStorage.get("key"));</script>
NB! json2.js support is also included - but in this case you still need to define the dollar $ object (but no methods of it), since $ is required by the script.
<script src="json2.js"></script>
<script>
$ = {}
</script>
<script src="jstorage.js"></script>
<script>alert($.jStorage.get("key"));</script>
© 2010 Andris Reinman, andris.reinman@gmail.com
jStorage is licensed under MIT-styled license, so basically you can do whatever you want to do with it.