29

I've seen some code where they just do this:

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

This is shorter than doing a document selector but is it the same thing?

Crescent Fresh
  • 115,249
  • 25
  • 154
  • 140
user169867
  • 5,732
  • 10
  • 39
  • 56

3 Answers3

34

Slight change:

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

Is equal to:

$(function() {});

As of jQuery 1.4: $().ready(function() { }); no longer works correctly in all cases. From the release notes:

As of jQuery 1.4, if you pass no arguments in to the jQuery() method, an empty jQuery set will be returned. In previous versions of jQuery, a set containing the document node would be returned.

Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • According to http://jquery14.com/day-01/jquery-14 it actually still works in 1.4 but has been deprecated. – Justin Ethier Mar 05 '10 at 04:01
  • 1
    @Justin Ethier - There are cases it fails to work, best to avoid it all together since it doesn't behave the same. For example if you tried to chain it with bind or trigger...works in 1.3, doesn't in 1.4. Anything deprecated, just pretend it doesn't exist if you want to be safer later. I updated the answer to be a bit clearer on this, it is a valid distinction in case someone's using it now – Nick Craver Mar 05 '10 at 11:23
  • 1
    It should also be noted that as of jQuery 3.0, all forms of ready except `$(fn)` [are deprecated](https://jquery.com/upgrade-guide/3.0/#deprecated-document-ready-handlers-other-than-jquery-function). – Useless Code Apr 18 '18 at 04:31
8

Nick and Justin have got the right answers here, but since we're on the topic, I would recommend for portability to never use $ in the global scope. A few too many libraries use it for their own purposes, and you could run into compatibility problems if you need to mix them up. Instead, you can use the optional first parameter to the jQuery ready handler:

jQuery(function($) {

});

This sets $ to be a reference to jQuery in that function scope.

nickf
  • 537,072
  • 198
  • 649
  • 721
  • In a project that's explicitly jQuery-based, there's absolutely no problem with using $ in the global scope. – Pointy Mar 05 '10 at 04:06
  • 5
    yeah, until it's not explicitly jQuery based. It's 6 extra characters to type and it future-proofs your code. Also, it may actually make the code run marginally faster, since the `$` reference is in local scope. – nickf Mar 05 '10 at 04:21
  • I agree in function scope this is true, out inside document.ready and such though I have to disagree that all that extra typing now replaces a find/replace of `$(` and `$.` later, I've had to de-scope jQuery before and a Ctrl+Shift+H was able to take care of it in about 20 seconds. +1, inside plugins and all it's especially important. – Nick Craver Mar 05 '10 at 11:21
7

According to the jQuery API docs, all three of the following syntaxes are equivalent:

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

So apparently that syntax will work, but is not the recommended syntax.

According to the jQuery 1.4 Release Notes:

In jQuery 1.3, jQuery() returned a jQuery set containing just the document. in jQuery 1.4, it returns an empty jQuery set. This can be useful for creating an empty set and adding elements to it dynamically. Note: The jQuery().ready() technique still works in 1.4 but it has been deprecated. Please use either jQuery(document).ready() or jQuery(function(){}).

Justin Ethier
  • 131,333
  • 52
  • 229
  • 284