jQuery API

.ready()

.ready( handler ) Returns: jQuery

Description: Specify a function to execute when the DOM is fully loaded.

  • version added: 1.0.ready( handler )

    handlerA function to execute after the DOM is ready.

While JavaScript provides the load event for executing code when a page is rendered, this event does not get triggered until all assets such as images have been completely received. In most cases, the script can be run as soon as the DOM hierarchy has been fully constructed. The handler passed to .ready() is guaranteed to be executed after the DOM is ready, so this is usually the best place to attach all other event handlers and run other jQuery code. When using scripts that rely on the value of CSS style properties, it's important to reference external stylesheets or embed style elements before referencing the scripts.

In cases where code relies on loaded assets (for example, if the dimensions of an image are required), the code should be placed in a handler for the load event instead.

The .ready() method is generally incompatible with the <body onload=""> attribute. If load must be used, either do not use .ready() or use jQuery's .load() method to attach load event handlers to the window or to more specific items, like images.

All three of the following syntaxes are equivalent:

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

There is also $(document).bind("ready", handler). This behaves similarly to the ready method but with one exception: If the ready event has already fired and you try to .bind("ready") the bound handler will not be executed.

The .ready() method can only be called on a jQuery object matching the current document, so the selector can be omitted.

The .ready() method is typically used with an anonymous function:

$(document).ready(function() {
  // Handler for .ready() called.
});

If .ready() is called after the DOM has been initialized, the new handler passed in will be executed immediately.

Aliasing the jQuery Namespace

When using another JavaScript library, we may wish to call $.noConflict() to avoid namespace difficulties. When this function is called, the $ shortcut is no longer available, forcing us to write jQuery each time we would normally write $. However, the handler passed to the .ready() method can take an argument, which is passed the global jQuery object. This means we can rename the object within the context of our .ready() handler without affecting other code:

jQuery(document).ready(function($) {
  // Code using $ as usual goes here.
});

Example:

Display a message when the DOM is loaded.

<!DOCTYPE html>
<html>
<head>
  <style>p { color:red; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
  <script>
  $(document).ready(function () {
  $("p").text("The DOM is now loaded and can be manipulated.");
});
  </script>

</head>
<body>
	<p>Not loaded yet.</p>
</body>
</html>

Demo:

Comments

  • Support requests, bug reports, and off-topic comments will be deleted without warning.

  • Please do post corrections or additional examples for .ready() below. We aim to quickly move corrections into the documentation.
  • If you need help, post at the forums or in the #jquery IRC channel.
  • Report bugs on the bug tracker or the jQuery Forum.
  • Discussions about the API specifically should be addressed in the Developing jQuery Core forum.
  • Wyr
    Since $(document).ready(handler) is the equivalent of $(handler) why is the longer form more popular. Not only in tutorials, but in code written by most developers. The resulting code will be slightly shorter, and arguably more readable, since most of the time $(function(){ will be in a single line
  • jh
    because the longer form is more descriptive and human readable
  • PLEASE REMOVE THIS COMMENT
  • egarcia
    Hey Eduárd, I asked on stackoverflow and got an answer - http://stackoverflow.com/questions/2238030/jque...
  • egarcia
    How do you trigger document.ready() manually? $('document').trigger('ready') doesn't seem to do the trick.
    I need to do this because I'm loading content (via ajax) that could have javascript on it, which contains document.ready() functions. I can't change the code of this external lib.
  • vivekjoy
    i have this same problem.if anyone gets a solution pls tell me
  • I have document.ready() working ok in the code loaded with ajax. You'd better create a post at the forum, e.g. http://forum.jquery.com/using-jquery/filter/pro... with the code showing the problem.
  • Eduárd
    This is my problem also. I'll try finding a solution tomorrow and write a comment about it here.