4

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?

1
  • 3
    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. Commented Oct 27, 2011 at 4:55

1 Answer 1

16

The three following syntaxes are allowed:

Syntax 1

$(document).ready(function)

Syntax 2

$().ready(function)

Syntax 3

$(function)

Update:

Additionally, from version 1.9 onwards:

$(window).on('load', null, function)
$(document).on('ready', null, function)
Sign up to request clarification or add additional context in comments.

2 Comments

There is also $(document).bind("ready", handler) (though that doesn't behave quite the same way).
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 ?

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.