Sunday, 20 March 2011

jQuery Document Ready Shorthand

You should always start your jQuery using the ready() function. The handler passed to .ready() function is guaranteed to be executed after the DOM is ready.

There are a few ways to start your jQuery function, but all are equivalent:

$(document).ready(function() {
  // Do some magic
});

$().ready(function() {
  // A bit quicker to write
});

$(function() {
  // Nice shorthand that I prefer to use
});

2 comments:

  1. There is a fourth...

    jQuery(function($) {
    // Nice shorthand that everyone should use!
    });

    ...so you know the $ symbol refers to the jQuery library (in case you use more than one).

    ReplyDelete