I am using jQuery and currently doing an $.ajax()
call inside the $(document).ready()
function. However I realized that to do the AJAX call I do not use anything from the DOM. Also that Ajax call might take a second or two (or more) server-side. So, for both those reasons, I'd like to get it going as soon as possible, and I'm thinking to refactor my code to look something like this (this would go in a <script>
block near the top of the HTML file):
var someGlobal="whatever";
...
function onReply(response){
//Do something
}
...
$.ajax({
'url':"call_me.json",
'type':'POST',
'data':{x:someGlobal,y:"Dude!"}
})
.fail(function(jqXHR,status){
console.log("AJAX ERROR:" + status);
})
.done(onReply);
...
However, the onReply()
callback does need the DOM to have been constructed (ditto for the .fail()
callback). So I don't want onReply
to be called before $(document).ready()
has run. Is there anything built-in to jQuery that allows me to guarantee that will be the case?
P.S. Using jQuery 1.10.x