Secret ingredients to quality software


Do you use the .ready() function?

Last updated by William Yin on 10 Sep 2015 11:22 pm (over 5 years ago) See History

Putting your initialization JavaScript code inside the .ready function is not always required, but it's much safer to do so.

jQuery exposes a .ready event which fires when the Document Object Model (DOM) is fully loaded and ready to be manipulated.

You can attach a function to this event so you can be sure the page is ready for you to work on.


Figure: Bad Example - if this jQuery is in the wrong place, the #login element may not exist!

$(function() {     $('#login').addClass('hidden'); });

Figure: Good Example - this code won't run until the DOM is fully loaded

Damian BradyDamian Brady

We open source. This page is on GitHub