Get AJAX data from server before document ready (jQuery)
I think that the problem is that you don't know exactly when the first function returns, since it'a asynchronous. So you should use the array in the callback only
function UseAjaxQueryForFillGlobalArray() { // make the call $.post(url, data, function() { // let's be sure that the dom is ready $(document).ready(function () { // use the array MakingInterfaceUsingGlobalArray(); } }}();// invoke the function
It's like reviving this post from the dead, but I had the same problem today, jQuery version greater than 1.6 has this ability:
https://api.jquery.com/jquery.holdready/
And I've used it like this:
$.holdReady(true);var remoteJSONContent = null;$.getJSON("http://www.example.com/remote.json", function(data) { remoteJSONContent = data; $.holdReady(false);});$(document).ready(function(){ console.log(remoteJSONContent);});
Without using holdReady
, I was getting null, after, I got the content.
For anyone still searching the answer for this.