Mudsaf Posted July 29, 2013 Share Posted July 29, 2013 (edited) Hello, i'm wondering how to store JSON array as javascript variable or something so visitor doesn't have to reload the file all the time. Method getting JSON <script>$(function() {$.getJSON('<myfolder/filename.js>', function(<something>) { /* store <something> */});});</script> "so visitor doesn't have to reload the file all the time", that was badly told. I mean if the script runs more than once at same page. Edited July 29, 2013 by Mudsaf Link to comment Share on other sites More sharing options...
justsomeguy Posted July 29, 2013 Share Posted July 29, 2013 You can store the result in a global variable, and if you need to access that information again then check if the variable exists first, or else load the file. Link to comment Share on other sites More sharing options...
thescientist Posted July 30, 2013 Share Posted July 30, 2013 (edited) var response = null; function getStuff(){ if(!response){ $.getJSON('path/to/file', function(data){ response = data; }) } return response;} Edited July 30, 2013 by thescientist Link to comment Share on other sites More sharing options...
Recommended Posts
Create an account or sign in to comment
You need to be a member in order to leave a comment
Create an account
Sign up for a new account in our community. It's easy!
Register a new accountSign in
Already have an account? Sign in here.
Sign In Now