What is the difference between the `$(document).ready()` function and `$(window).load()` function in jQuery?

1 Answers
Answered by suresh

jQuery Interview Question: Difference between $(document).ready() and $(window).load()

jQuery Interview Question: Difference between $(document).ready() and $(window).load()

In jQuery, the $(document).ready() function and $(window).load() function are commonly used for executing JavaScript code when the DOM is fully loaded. However, there is a key difference between them.

The $(document).ready() function is used to execute code once the DOM is fully loaded, but before the images and other external resources are fully loaded. This means that it's suitable for manipulating DOM elements or attaching event handlers that do not rely on the content being fully loaded.

On the other hand, the $(window).load() function is used to execute code only after all the content on the page, including images and other external resources, has finished loading. This is useful for tasks that require all resources to be available, such as working with image dimensions or performing calculations based on the complete content of the page.

In summary, $(document).ready() is fired once the DOM is ready, while $(window).load() is fired after everything on the page has fully loaded.

Answer for Question: What is the difference between the `$(document).ready()` function and `$(window).load()` function in jQuery?