views:

49

answers:

3

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?

+2  A: 

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
How is it that when I first saw your answer I thought "Ah, that's just brief, I can expand on some of that" and when I look back having posted mine its a lot longer yet it doesn't suggest you've edited it? I'll note that fortunately I expanded on something different to you so I'm going to leave my answer there unless you want to copy relevant details into yours in which case I will remove mine...
Chris
@Chris - I added a bit of content, for 5 minutes after an edit or initial post, a post won't show as "edited".
Nick Craver
@Nick Craver: Ah, that's cunning. And a bit sneaky for getting in the "oldest answer" without having to put a full answer in first time round. :)
Chris
Nick Craver
@Nick: Ah yes, don't get me wrong, I wasn't suggesting you were "frist!" posting or anything like that. Just that my usual scoring for two identical answers is to give the + to either both or whoever posted first. It just gets confusing for me now I know that the answered time isn't necessarily the same as the last edited time. :) Anyway, I'm sure this isn't the place for this kind of conversation. ;-)
Chris
+1  A: 

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:

(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
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?
qwertypants
@qwertypants - Nope, it happens when the DOM is ready, later in most cases (unless this code was executed after the DOM was already ready).
Nick Craver
@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
A: 

This link has a good explanation on how the first two are different.

Hector