Adding Initialization Code in jQuery

$(document).ready(function(){   (initialization code here...) } or $(document).load(function(){   (initialization code here...) }

In jQuery, you can trigger and execute initialization code at two different times: when the DOM is ready, and when the document and its resources have fully loaded. The option to use depends on what needs to happen in your initialization code.

Using the .ready() jQuery method triggers the initialization code to run when the DOM is fully ready. All the DOM objects will be created, and the page will be displayed to users. Note that not all page resources, such as images, may have fully downloaded at this point. This is the option that I use most frequently ...

Get jQuery and JavaScript Phrasebook now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.