4

On the jQuery site, the description for $(callback) was that it behaves the same as $(document).ready(function) but then the examples showed some differences between the two syntaxes. So I was wondering, does anyone know exactly what the differences between the two are?

Sparky
98.8k26 gold badges202 silver badges293 bronze badges
asked Aug 15, 2011 at 18:22
1
  • What differences are you talking about? Commented Aug 15, 2011 at 18:37

4 Answers 4

7

There are no differences, and the docs don't show any difference:

All three of the following syntaxes are equivalent:

  • $(document).ready(handler)
  • $().ready(handler) (this is not recommended)
  • $(handler)

Straight from: http://api.jquery.com/ready/

I think you are confused by the example showing jQuery(function($){ ... }); Which is just a way of calling $(handler), with no $ conflict.

IE.

// Here `$` is used by another library
jQuery(function($){
 // Here `$` refers to jQuery
});
answered Aug 15, 2011 at 18:26
Sign up to request clarification or add additional context in comments.

Comments

4
$(document).ready(function() {
 // Handler for .ready() called.
});

Which is equivalent to calling:

$(function() {
 // Handler for .ready() called.
});

http://api.jquery.com/ready/

answered Aug 15, 2011 at 18:24

Comments

1

There is no difference at all, except that the shortcut is very slightly slower as it has to decide the type of the argument and then call $(document).ready. (Actually the source code of jQuery is very clean so you can easily check for yourself - $() calls $.fn.init, which goes through a couple of tests then calls ready at line 177.)

answered Aug 15, 2011 at 18:37

Comments

0

There is no difference. If you call $() with just one parameter - a function: $(some_function) - it means, that it will call $(document).ready(some_function)

So, for simplicity, you could use:

$(function(){
// your code
});

P.S. Don't use this structure if you are using different libraries (that can conflict with $ variable). In these cases use:

jQuery(function(){
// your code
});
answered Aug 15, 2011 at 18:33

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.