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