zjrJS Javascript Library

zjrJS.Doc.Store.get() and .set() - zjrJS Javascript Library

This information lives on a webpage hosted at the following web address: 'https://omegajunior.globat.com/code/zjrjs/'.

How to use zjrJS.Doc.Store.get() and .set().

Declaration
zjrJS.Doc.Store.get(String key);
zjrJS.Doc.Store.set(String key, Any value);


Goal
To store data in the visitor's browser. Subsequently testing HTML5's localStorage, MS userData, and falling back to trusty old cookies.

Typical usage
Your web application needs to remember something in-between page navigation, or needs to cache often-used information form performance reasons or for off-line use. Combined with zjrJS.Doc.lS() (or an AJAX method of your choosing) to retrieve information, and a trivial check for cache expiration, zjrJS.Doc.Store may prove a tremendous help for optimizing both storage and performance.

Obligatory Privacy Talk
Since May 2011, European Law requires a web site to request explicit consent from their visitors before they set most cookies, the exception being cookies needed for services requested explicitly by that visitor. The consent requirement also covers data set using other technologies, like the localStorage employed here.

Chances are your visitors do wish to enjoy the speed and other functionality gained by allowing data to be stored on their computer.Nonetheless, you need to tell your visitors, explicitly ask for their consent, and react accordingly.

Member Type
Function.

Value Type
For .get(): String (whatever value was set, stringified automatically if needed).
For .set(): Boolean or Null. True if value was set successfully.

Parameters
String key: a name of your choosing, unique to your web page's or app's URL.
Any value: anything you need to store, as long as it can be stringified.

Dependencies
zjrJS.Doc.aC()
zjrJS.Doc.cE()
zjrJS.Doc.eid()

Example
<script type="text/javascript" src="zjrJS.version.js" id="zjrJS"></script>
<script type="text/javascript">/* <![CDATA[ */
var my=({
my.Lyric=({"one-two":"Buckle my shoe", "three-four":"Open the door"});
});
zjrJS.Doc.aE(window,"load",function(){
var S=zjrJS.Doc.Store;
S.set("motherGoose", my.Lyric);
S=null;
});
zjrJS.Doc.aE(zjrJS.Doc.eid("ButtonAlertLyric","click",function(){
var S=zjrJS.Doc.Store;
alert(S.get("motherGoose"));
S=null;
});
/* ]]> */</script>


Considerations
Even though localStorage can request to store megabytes of information, understand that we fall back to cookies in case no other method is available... and browsers generally limit the amount of data a cookie can hold. So, if you need 10MB like NYTimes' Skimmer, implement your own storage routine.

Download:

Download zjrJS’ latest stable build (20141202t2322) minified (7.1KB).

Download zjrJS’ latest stable build (20141202t2322) (11.9KB).

Clicky Analytics