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()');
});
There is no difference.
$is the same asjQuery. If you view the unminified source, you will seevar $ = jQuery = ...or something to that effect.The
jQueryfunction checks the type of it’s parameter, if it is a function, it treats it the same as$(document).ready(...)Calling
jQuerywithout a parameter defaults to usingdocument. So$()and$(document)are identical. Try it in Firebug.