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.
3 Answers 3
Just put all your other code inside function(iconData) {icons = iconData;}); callback function, it will be executed after the JSON will be fetched.
2 Comments
$.getJSON? What if I'd where to use $.getJSON twice..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()});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?
3 Comments
Head and it would work..?The code loaded on the head, and then set to synchronize loading
$.ajaxSettings.async = false;
$.getJSON(url, data, function(data){ });
3 Comments
[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/.
$.when-> api.jquery.com/jquery.when$.getJSONapi.jquery.com/jquery.getjson Is this what you are looking for?