1

I'm trying to load the JSON before anything else loads, because I need to access the JSON immediately when a browser connects to the server.

This is how I load my json

$.getJSON('icons.txt', function(iconData) {
 icons = iconData;
});

How can I make it so javascript loads the json first before anything else? or at least makes it so that it's loaded before the user connects.

asked Aug 6, 2017 at 16:33
1
  • $.getJSON returns a promise object. If you want to do something after 1 or multiple json files are loaded, make use of $.when api and pass the promise objects returned from $.getJSON calls, $.when will give you a callback after all the jsons are loaded. $.when -> api.jquery.com/jquery.when $.getJSON api.jquery.com/jquery.getjson Is this what you are looking for? Commented Aug 7, 2017 at 7:03

3 Answers 3

0

Just put all your other code inside function(iconData) {icons = iconData;}); callback function, it will be executed after the JSON will be fetched.

answered Aug 6, 2017 at 16:36
Sign up to request clarification or add additional context in comments.

2 Comments

I should put all my code inside the $.getJSON? What if I'd where to use $.getJSON twice..
@MartijnEbbens, if you want to execute some code AFTER the JSON will be fetched, you need to put this code inside callback function (function(iconData) {icons = iconData;}); in your case). .getJSON is an asynchronous operation, if you will just put the code after it, this code will be executed before the JSON fill be fetched. If you want to call getJSON twice or more, you can wrap other code in function and invoke it like this: $.getJSON('icons.txt', function(iconData) {stuffToDoAfterJsonFetched()});
0

If you put your tag in the head of your HTML, it should be executed at first.

Maybe duplicate of: Load javascript before rendering page?

answered Aug 6, 2017 at 16:37

3 Comments

How can I then access the JSON from the file?
Well you can use the dom after to use the json you just fetch.
So I could just paste the code in the HTML Head and it would work..?
0

The code loaded on the head, and then set to synchronize loading

$.ajaxSettings.async = false;
$.getJSON(url, data, function(data){ });
answered Aug 6, 2017 at 16:48

3 Comments

How can I then access the JSON from the file?
Yeah, that works but I do get a Deprecation. [Deprecation] Synchronous XMLHttpRequest on the main thread is deprecated because of its detrimental effects to the end user's experience. For more help, check https://xhr.spec.whatwg.org/.
This error is understandable because you are loading a file synchronously and will wait for the file to be loaded after the code continues.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.