Skip to content

Instantly share code, notes, and snippets.

@townivan
Last active December 11, 2015 15:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save townivan/ef7382e399e7432de60a to your computer and use it in GitHub Desktop.
Save townivan/ef7382e399e7432de60a to your computer and use it in GitHub Desktop.
JS load external JSON into localstorage
<!doctype html>
<html>
<head>
<meta charset="utf-8">
<title>Calculator</title>
<style>body{display:none;}</style>
<script src="//code.jquery.com/jquery-1.11.3.min.js"></script>
<script>
function loadMyJson() {
return $.getJSON('_config.json');
}
$.when(loadMyJson()).then(function (data) { // async getJSON() complete!
// save into sessionStorage to be used on template page
var localData = JSON.stringify(data);
window.sessionStorage.setItem('myjson', localData);
// now load() our template and show the page when load() completes
$('#templateDIV').load( '../_jQLife/calculator.html', function() {
$('body').show(); // to avoid FOUC
});
}); //loadMyJson()
</script>
</head>
<body>
<div id="templateDIV">&nbsp;</div>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment