Introducing $(document).ready()

read 134 comments

This is the first thing to learn about jQuery: If you want an event to work on your page, you should call it inside the $(document).ready() function. Everything inside it will load as soon as the DOM is loaded and before the page contents are loaded.

JavaScript:
  1. $(document).ready(function() {
  2.     // put all your jQuery goodness in here.
  3. });

The $(document).ready() function has a ton of advantages over other ways of getting events to work. First of all, you don't have to put any "behavioral" markup in the HTML. You can separate all of your javascript/jQuery into a separate file where it's easier to maintain and where it can stay out of the way of the content. I never did like seeing all those "javascript:void()" messages in the status bar when I would hover over a link. That's what happens when you attach the event directly inside an <a href> tag.

On some pages that use traditional javascript, you'll see an "onload" attribute in the <body> tag. The problem with this is that it's limited to only one function. Oh yeah, and it adds "behavioral" markup to the content again. Jeremy Keith's excellent book, DOM Scripting, showed me how to create an addLoadEvent function to a separate javascript file that allows for multiple functions to be loaded inside it. But it requires a fair amount of code for something that should be rather straightforward. Also, it triggers those events when the window loads, which leads me to another advantage of $(document).ready().

With $(document).ready(), you can get your events to load or fire or whatever you want them to do before the window loads. Everything that you stick inside its brackets is ready to go at the earliest possible moment — as soon as the DOM is registered by the browser, which allows for some nice hiding and showing effects and other stuff immediately when the user first sees the page elements.

Update

I thought it might help the absolute beginners to see an example of the <head> where you would include your scripts. Note that CSS stylesheets come before the jQuery file, and the jQuery file comes before your custom script:
HTML:
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN"
  2.  "http://www.w3.org/TR/html4/strict.dtd">
  3. <html lang="en">
  4. <head>
  5.   <meta http-equiv="Content-Type" content="text/html; charset=utf-8"></meta>
  6.   <title>your title</title>
  7.   <link rel="stylesheet" type="text/css" href="styles/forms.css"></link>
  8.   <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.3/jquery.min.js"></script>
  9.   <script src="yourcustomscript.js" type="text/javascript"></script>
  10. </head>
  11. <body>
  12.   <!-- the body of your document goes here -->
  13. </body>
  14. </html>

It is in "yourcustomscript.js" that you would place your script inside the $(document).ready() block.

comment feed

134 comments

  1. Pete

    Karl & Jörn (first commenter) - Many thanks for the explanation - I just inherited some web code that had the javascript $(document).ready code and the shorter version. It was great to see that they are one and the same.

  2. Good read, but a small correction, you can have mutliple functions in the onload in a body tag...

    <body onload="this();that();anotherOne()">

    This works fine, but yes, it does add extra markup

25 Pings

  1. [...] la chiamata al nostro helper nell'evento "document ready" di jQuery in modo da costruire la griglia al momento [...]

  2. [...] use jQuery’s handy document ready to set up new Fling subscribers, and then publish the [...]

  3. [...] of the first things that you should learn about JQuery is the usage of $(document).ready(): This is the first thing to learn about jQuery: If you want an event to work on your page, you [...]

  4. [...] Introducing $(document).ready() » Learning jQuery - Tutorials and Information [...]

  5. [...] above pushlog.tmpl code is placed inside $(document).ready(). As you can see I basically hide all the files touched data on line 40. Then, I go through each [...]

  6. [...] new code. Lots of changes to look at it. First of all the JavaScript code has been shifted within $document.ready(). The major changes have occurred in loadEntries(). The jquery library makes it extremely simple to [...]

  7. [...] event handler of either the window or body HTML DOM objects in JavaScript — except that $(document).ready() does not require us to hard-code it into an XHTML [...]

  8. [...] ir al código que hará que todo se conecte. Debajo de todo (aunque tengo teorías de que con un $(document).ready() de jQuery seria lo mismo) ponemos [...]

  9. [...] For more infomation: Introducing $(document).ready() [...]

  10. [...] Il codice di jQuery deve sempre iniziare con il costrutto $(document).ready(), per segnalare alla pagina di eseguire il codice quando tutto il contenuto è caricato: [...]

  11. [...] Introducing $(document).ready() » Learning jQuery – Tutorials and Information [...]

  12. [...] You can read more about it : Introducing $(document).ready() [...]

  13. [...] You can read more about it : Introducing $(document).ready() [...]

  14. [...] $(document).ready() bliver normalt kaldt, når DOM er klar til at blive manipuleret, men det sker ikke efter at et [...]

  15. [...] Now one of the most important features of jQuery - the $(document).ready() function. What it basically does is it allows us to run our Javascript stuff as soon as the DOM is ready, and does not wait for the entire page to load (including images, and other resources which might be time consuming). This means that our scripts can execute a bit faster when compared to using the traditional window.onload method. More on that here and here. [...]

  16. [...] to jQuery then you need to start by understanding $(document).ready(). You can read all about it here. But the bottom line is that everything inside this method will load as soon as the DOM is loaded [...]

  17. [...] you want to know more about this, you can visit one good site named “learningjquery“. Karl Swedberg has explained clearly about the same with an example of how to start coding [...]

  18. [...] inside the document ready function gets executed when jQuery determines that your document is fully loaded. This is an [...]

  19. [...] To check whether the HTML is loaded, use the following call. That makes sure that the DOM is loaded and you can browse the DOM via jQuery and now the real work of the script can start. I strongly recommend to use this variant. I don’t want to explain all the benefits, but you can read about it in the article Introducing $(document).ready(). [...]

  20. [...] To check either a HTML is loaded, make make make make make make make make use of of of of of of of of a following call. That creates certain which a DOM is installed as well as we can crop a DOM around jQuery as well as right away a genuine work of a book can start. we strongly suggest to make make make make make make make make use of of of of of of of of this variant. we do not wish to insist all a benefits, though we can review about it in a essay Introducing $(document).ready(). [...]

  21. [...] If you would like to know more about $(document).ready(function(){}); – click here and if you would like know when to use $(window).load(function(){}); – click here to [...]

  22. [...] syntax for jQuery. If you are using jQuery, you can use $(document).ready() instead of the body’s onload event and the checkJS() function. All you need to do is add this [...]

Leave a Comment

IMPORTANT:

  • If you wish to post code examples, please wrap them in <code> tags.
  • Multi-line code should be wrapped in <pre><code> </code></pre>
  • Use &lt; instead of < and &gt; instead of > in the examples themselves. Otherwise, you could lose part of the comment when it's submitted.