JavaScript

Difference Between window.onload and $(document).ready()

The “window.onload” and the “$(document).ready()” are used to execute JavaScript code when the page finishes loading, but the main difference is when they execute and how they are used. “window.onload” waits for all assets on the page to finish loading, including images and other external resources, whereas “$(document).ready()” waits for the DOM to be fully loaded but doesn’t wait for external resources like images to finish loading.

This post will define the difference/variations between “window.onload” and the “$(document).ready()”.

Difference Between “window.onload” and “$(document).ready()”

window.onload” and “$(document).ready()” are different in executing JavaScript code when the page finishes loading. The following are the key variations between these two:

“window.onload” “$(document).ready()”
“window.onload” is a built-in JavaScript event. “$(document).ready()” is specific for jQuery library.
It waits until all of the page’s assets have finished loading. It waits for the DOM to be fully loaded but doesn’t wait for external resources.
It can cause slower page load times if heavy resources are being loaded. Faster, as it doesn’t wait for all resources to be loaded.
Only one function can be assigned. Multiple functions can be assigned and executed in order.
Compatible with all browsers. Requires jQuery library.
Not recommended to attach event listeners, as it may cause performance issues. Recommended for attaching event listeners to DOM elements.

 

How to Use the “window.onload()” Method in JavaScript?

The “window.onload()” method is commonly used in JavaScript to perform actions or execute code after the page has finished loading.

Syntax

The following syntax is utilized for the JavaScript “window.onload” method:

window.onload = function() {
 // code
};

 

Or you can also use it as:

function funcName() {
 // code
}
window.onload = funcName();

 

Example

Call the “window.onload” event with a function “loading()” to show an alert() message on the page load:

window.onload = function loading() {
 alert("Page is Successfully loaded");
};

 

The output indicates that the function on the onload event will display after loading page:

How to Use “$(document).ready()” Function in JavaScript?

$(document).ready()” function is an important part of the jQuery library. It executes JavaScript code as soon as the DOM is prepared for manipulation after the document has been loaded into the browser.

Syntax

Use the given syntax for the ready() function:

$(document).ready(function() {
 // code
});

 

Example

For using the “$(document).ready()” function, first add the jQuery library in the <head> tag:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.3/jquery.min.js"></script>

 

Call the ready function to show an alert message while DOM is ready to manipulate:

$(document).ready(function(){
 alert("Page is Successfully loaded");
});

 

Output

That’s all about the “window.onload” and “$(document).ready()” function in JavaScript.

Conclusion

window.onload” is a JavaScript “event” while the “$(document).ready()” is a “function” specific to the jQuery library. Both are being used to execute JavaScript code when the page finishes loading. The key distinction/variation between these two is that the “window.onload” waits for loading all assets on the page to finish loading. While the “$(document).ready()” waits for the DOM to be fully loaded but doesn’t wait for external resources. This post described the difference between “window.onload” and the “$(document).ready()” in JavaScript and its usage with examples.

About the author

Farah Batool

I completed my master's degree in computer science. I am an academic researcher and love to learn and write about new technologies. I am passionate about writing and sharing my experience with the world.