Take the 2-minute tour ×

I use a lot of jQuery, so I have to keep typing out the $(document).ready function to put the jQuery code. Is there a shorter form of the function?

share|improve this question
Why are you making lots of $(document).ready() blocks? Only initialization code that's referencing the DOM on first page load needs to be in such a block. You can also puts lots of pieces of code in one $(document).ready() block. –  jfriend00 Oct 27 '11 at 4:55
The answers below are clear, but it's also spelled out pretty clearly within the jQuery doco for document ready. –  nnnnnn Oct 27 '11 at 5:29
add comment

2 Answers

up vote 13 down vote accepted

The three following syntaxes are allowed:

Syntax 1


Syntax 2


Syntax 3



Additionally, from version 1.9 onwards:

$(window).on('load', null, function)
$(document).on('ready', null, function)
share|improve this answer
+1 Very nice answer. Welcome to Stack Overflow! –  Paul Sasik Oct 27 '11 at 4:48
There is also $(document).bind("ready", handler) (though that doesn't behave quite the same way). –  nnnnnn Oct 27 '11 at 5:31
The first is good, the second is in danger with conflicting another JS framework/library. The third is the same as second. You should use jQuery(document).ready(function() {...}); Write that line once and implement all your logic between square brackets. What's the problem ? –  lukas.pukenis Oct 27 '11 at 6:10
add comment

This works:

$(function() {
    // enter code here
share|improve this answer
add comment

Your Answer


