Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a difference between $().ready() and $(document).ready()

Tags:

jquery

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?

like image 937
user169867 Avatar asked Mar 05 '10 02:03

user169867


People also ask

What is difference between $( function () and document Ready?

So technically they are both the same. Not major difference between these two declaration. They used based on weather you use JavaScript then you should use $(document). ready declaration in other case you use jQuery library which is a part of JavaScript then you should use $(function) declaration.

What is the difference between $( window .load and $( document ready ()?

ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc. are loaded, but after the whole DOM itself is ready.

What is $( document ready () function Why should you use it?

The ready() method is used to make a function available after the document is loaded. Whatever code you write inside the $(document ). ready() method will run once the page DOM is ready to execute JavaScript code.

What is $( document ready () equivalent in JavaScript?

jQuery $(document). ready() Equivalent in JavaScript This event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading.


3 Answers

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.

like image 115
Nick Craver Avatar answered Oct 14 '22 03:10

Nick Craver


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.

like image 27
nickf Avatar answered Oct 14 '22 03:10

nickf


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(){}).

like image 37
Justin Ethier Avatar answered Oct 14 '22 03:10

Justin Ethier