45

I have seen some shortcuts for the ready() method and would like to know which actually happens first, because my test results confuse me..

$(document).ready(function(){
    alert("document ready");
});

$(window).load(function(){
    alert("window ready");
});

(function($){
    alert("self invoke");
})(jQuery);

Here self invoke happens first, then document, then window. Is the self invoke technique considered a ready() method?

Wilkins
  • 800
  • 2
  • 6
  • 12

3 Answers3

102

The third option is not a shortcut for .ready() (or jQuery related really), the self invoke runs immediately (as soon as it appears in the code), this is probably the shortcut you're thinking of though:

$(function(){
  alert("I'm a ready shortcut");
});

Passing a function into $(func) is a shortcut for $(document).ready(func);. The no-conflict version would look like this:

jQuery(function($) {
  //$ is jQuery
});
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
13

Nick Craver is right in what he says but I think it is worth noting that in that last example that it isn't actually doing anything with jquery at all. jQuery is being passed as a parameter to the anonymous function but the function isn't doing anything with it.

The last example is equivalent to an Immediately-Invoked Function Expression (IIFE):

(function(){
    alert("self invoke");
})();

And clearly this is just immediately calling the anonymous function as soon as that line of code is being hit and thus doing the alert. It isn't invoking jQuery at all which is why Nick is right when he says it is defintiely not a ready() method.

Chris
  • 27,210
  • 6
  • 71
  • 92
  • True, it's just a self invoking method, not part of jQuery. That being said, why does it happen before the ready() method? Shouldn't jQuery's ready method happen before a self invoking method? – Wilkins Oct 11 '10 at 15:28
  • 1
    @qwertypants: all of those lines of code are executed immediately the browser finds them. The difference is that the first two when run just add an event handler through jquery to the load and ready events. The last one just executes the anonymous function to do an alert(). This is exactly why we use ready functions because the immediate execution is usually not what we want because the dom might not be ready, etc. – Chris Oct 11 '10 at 15:46
6

This article has a good explanation on how the first two are different:

$(document).ready vs. $(window).load

jQuery offers two powerful methods to execute code and attach event handlers: $(document).ready and $(window).load. The document ready event executes already when the HTML-Document is loaded and the DOM is ready, even if all the graphics haven't loaded yet. If you want to hook up your events for certain elements before the window loads, then $(document).ready is the right place.

$(document).ready(function() {
  // executes when HTML-Document is loaded and DOM is ready
  alert("document is ready");
});

The window load event executes a bit later when the complete page is fully loaded, including all frames, objects and images. Therefore functions which concern images or other page contents should be placed in the load event for the window or the content tag itself.

$(window).load(function() {
  // executes when complete page is fully loaded, 
  // including all frames, objects and images
  alert("window is loaded");
});
Floern
  • 33,559
  • 24
  • 104
  • 119
Hector Correa
  • 26,290
  • 8
  • 57
  • 73