Can I get a jQuery Deferred on document.ready()? Can I get a jQuery Deferred on document.ready()? ajax ajax

Can I get a jQuery Deferred on document.ready()?


You can associate a deferred object with the document using data(), and resolve() it in your ready handler. This way, you should be able to use the stored deferred object with $.when():

$(document).data("readyDeferred", $.Deferred()).ready(function() {    $(document).data("readyDeferred").resolve();});$.when($.ajax("translations"), $(document).data("readyDeferred")) .then(function() {    // Start doing stuff here.});


Here's a cleaned up version of ircmaxell's comment:

(function() {  var doc_ready = $.Deferred();  $(doc_ready.resolve);  $.when(doc_ready, $.ajax('translations')).then(function() {    console.log("done");  });})();

edit

Some clarification to stop the incorrect edits:

Passing a function to the jquery object (e.g. $(some_func)) is the same as $(document).ready(some_func).

Therefore, the $(doc_ready.resolve); line is just shorthand for something like this:

$(document).ready(function() {  doc_ready.resolve()});


Try this:

$.when($.ajax('translations'), $.ready).then(function() {    // Start doing stuff here});