3

personally I prefer the first syntax

jQuery() 

is it safe respect to the common used:

$(document).ready() 

For the other selector i will anyway use $('#id'). i am just asking for the first .ready

7 Answers 7

4

jQuery and $ are interchangeable (from jQuery source):

window.jQuery = window.$ = jQuery 

but be careful with other libraries that might use $ as well. As for using ready():

All three of the following syntaxes are equivalent:

  • $(document).ready(handler)

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

  • $(handler)

From ready() API.

Sign up to request clarification or add additional context in comments.

1 Comment

I know its copied verbatim but the "this is not recommended" should really be "this is not supported anymore"
3

Right after you loaded jquery script add this code:

(function($){ $.fn.oldReady = $.fn.ready; $.fn.ready = function(fn){ return $.fn.oldReady( function(){ try{ if(fn) fn.apply($,arguments); } catch(e){}} ); } })(jQuery); 

Comments

2

Its "safer" to use jQuery, as it won't conflict with other frameworks such as Mootools. If you only use jQuery, there is no harm in using $.

1 Comment

It's not safer if you don't also set up noConflict mode.
2

If you're only using the jQuery framework, then I'd go with the $ syntax, in particular the short-hand version for the ready event handler as being cleaner. This is only a personal preference -- it's immediately obvious to me what it does and it's not as verbose. You can choose any of the other forms.

$(function() { }); 

If you have other frameworks, then you should follow the instructions to set up jQuery in noConflict mode: http://api.jquery.com/jQuery.noConflict/

Code sample from the referenced page:

<script type="text/javascript" src="other_lib.js"></script> <script type="text/javascript" src="jquery.js"></script> <script type="text/javascript"> $.noConflict(); jQuery(document).ready(function($) { // or jQuery(function($) { // Code that uses jQuery's $ can follow here. }); // Code that uses other library's $ can follow here. </script> 

If you're building your own plugin and you can't be sure that jQuery is the only framework in use then, I'd use jQuery() internally or at least assign $ to the jQuery object explicitly within your scope so that you know what you're working with.

1 Comment

You're the only one who's mentioned that you can pass an argument to .ready (or its equivalent) to act as the jQuery variable.
1

The two names $ and jQuery are synonyms. jQuery is the more explicit; $ may be overridden if you use another framework.

If you pass a function as the first argument to your call to the jQuery function (or, obviously, to $), it is executed exactly as if it were a call to jQuery(document).ready. So yes, it is exactly the same.

Indeed, all these are functionally equivalent (provided you don't have anything else mucking around with $):

$(document).ready(function() {}); jQuery(document).ready(function() {}); $(function() {}); jQuery(function() {}); 

It is entirely a matter of circumstance and style as to which you use.

My personal preference is to use the explicit $(document).ready call (#1): it is obvious that this is code that will be run when the DOM is ready. The major advantage of your preference (#4) is that it clearly denotes that the code is jQuery, which may be useful for someone reading your code in future.

2 Comments

I think the more readeable is the jQuery(); I will stick with it
the jQuery(document).ready is pretty too! but maybe too verbouse than only jQuery(); The difficult I have here is because i come from PHP, and $ are only used for variables not for functions.
0

jQuery documentation says they are the same.

Comments

0

Your question is really two in one. There are several ways of hooking up the ready event, but these four variations are relevant to the question:

jQuery(function(){ ... }); jQuery(document).ready(function(){ ... }); $(function(){ ... }); $(document).ready(function(){ ... }); 

The $ identifier is an alias for the jQuery identifier. Using the noConflict method you could free up the $ alias if it conflicts with any other library and only use the jQuery identifier (or even free up all identifiers and specify one of your own).

Using the jQuery object itself for hooking up the ready event is a shorthand that is commonly used, but it's less clear from just looking at the code what it does.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.