I've seen four different ways to tell jQuery to execute a function when the document is ready. Are these all equivalent?
$(document).ready(function () {
alert('$(document).ready()');
});
$().ready(function () {
alert('$().ready()');
});
$(function () {
alert('$()');
});
jQuery(function ($) {
alert('jQuery()');
});
Also it should be mentioned, that symbol that you pass to function will be use inside the function. For example:
if you want use $ - you can leave function's param in this situation empty
The real example you can find here http://jsfiddle.net/yura_syedin/BNgd4/
Here's another one - starts like this...
then to finish...
An example is here: http://jsfiddle.net/C2qZw/23/
There is no difference.
$
is the same asjQuery
. If you view the unminified source, you will seevar $ = jQuery = ...
or something to that effect.The
jQuery
function checks the type of it's parameter, if it is a function, it treats it the same as$(document).ready(...)
Calling
jQuery
without a parameter defaults to usingdocument
. So$()
and$(document)
are identical. Try it in Firebug.re: Geroge IV's comments regarding $() == $(document) its correct. From the unminified source (init is what get called internally):
Also from source, to back up previous conversations:
this should be community wiki. I've always been interested in the inner workings of jquery, now I've had an excuse to start looking :-)