Wednesday, June 25, 2014

JavaScript: Easy way to Cache objects in Local Storage/ Client Browser in JavaScript

Here is a easy way to store and retrieve  your java script objects ( even more than 4KB) in the local /Client browser . I'm using jStorage for this method and jStorage supports storing Strings, Numbers, JavaScript objects, Arrays and even native XML nodes. jStorage also supports setting TTL values for auto expiring stored keys and - best of all - notifying other tabs/windows when a key has been changed or publishing/subscribing to events from the same or another tab/window, which makes jStorage also a local PubSub platform for web applications.


 < script src="http://cdnjs.cloudflare.com/ajax/libs/json2/20110223/json2.js">
< script src="http//code.jquery.com/jquery-1.9.1.min.js">
< script src="jstorage.js">


< script type="text/javascript">
var testObject = "test123"; /* Sample Data. You can fetch the objects from Server and store it*/

// Check if "key" exists in the storage
var value = $.jStorage.get("key");
if(!value){
    $.jStorage.set("key",testObject);
}
< /script >


Here you go, Now its easy to store to the cache with bigger data. hope this helps.
Article Source and .js file available at : www.jstorage.info

Happy Browsing!
Relax..

No comments:

Post a Comment