Tutorials:Introducing $(document).ready()
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. $(document).ready(function() {
// put all your jQuery goodness in here.
});
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().
Tutorials:Multiple $(document).ready()
Some advanced tips for working with $(document).ready()
One more great thing about $(document).ready() that I didn't mention in my previous tutorial is that you can use it more than once. In fact, if you don't care at all about keeping your code small, you could litter your JavaScript file with them.
It's great to be able to group your functions within a file or even across multiple files, and jQuery's flexible $(document).ready() function allows you to do that, pain free.
You could, for example, have one .js file that is loaded on every page, and another one that is loaded only on the homepage, both of which would call $(document).ready(). So, inside the <head> tag of your homepage, you would have three references to JavaScript files altogether, like so:
<script src="/scripts/jquery.js"></script> <script src="/scripts/common.js"></script> <script src="/scripts/homepage.js"></script>
$(document).ready(function() {
// some code here
});
$(document).ready(function() {
// other code here
});
$(function() {
// do something on document ready
});
Examples
Example 1
Displays a message, when DOM is ready:$(document).ready(function () {
$("p").text("The DOM is now loaded and can be manipulated.");
});
Example 1 - Full source:
<!DOCTYPE html> <html> <head> <script src="http://code.jquery.com/jquery-latest.js"></script> <script type="text/javascript" language="javascript"> $(document).ready(function(){ $(document).ready(function () { $("p").text("The DOM is now loaded and can be manipulated."); }); }); </script> <style>p { color:red; }</style> </head> <body> <p> </p> </body> </html>
Example 2
Executes the function when the DOM is ready to be used.$(document).ready(function(){
// Your code here...
});
Example 3
Uses both the shortcut for $(document).ready() and the argument to write failsafe jQuery code using the $ alias, without relying on the global alias.jQuery(function($) { // Your code using failsafe $ alias here... });
Example 4
Usually write like this:$(function() {
// Your code here...
});

