http://jsfiddle.net/LyAcs/7/
Given a data.json file containing:
[ { "myKey": "A", "status": 0, "score": 1.5 }, { "myKey": "C", "status": 1, "score": 2.0 }, { "myKey": "D", "status": 0, "score": 0.2 }, { "myKey": "E", "status": 1, "score": 1.0 }, { "myKey": "F", "status": 0, "score": 0.4 }, { "myKey": "G", "status": 1, "score": 3.0 } ]
JS conditional function with parametric localStorage.target:
function loadFileJSON( toLocalStorage, fromUrl){ if (localStorage[toLocalStorage]) { console.log("Good! Data already loaded locally! Nothing to do!"); } else { $.getJSON( fromUrl , function(data) { localStorage[toLocalStorage] = JSON.stringify(data); console.log("Damn! Data not yet loaded locally! Ok: I'am loading it!"); }); } }
Read the data: Now your data is now in localStorage.myData , as a string. You can access it using:
var myJSON = JSON.parse(localStorage.myData);
Then the ability to record this local data is more interesting.
Hugpz source share