window.onload vs $(document).ready()
window.onload vs $(document).ready()
Question
What are the differences between JavaScript's window.onload
and jQuery's $(document).ready()
method?
Accepted Answer
The ready
event occurs after the HTML document has been loaded, while the onload
event occurs later, when all content (e.g. images) also has been loaded.
The onload
event is a standard event in the DOM, while the ready
event is specific to jQuery. The purpose of the ready
event is that it should occur as early as possible after the document has loaded, so that code that adds functionality to the elements in the page doesn't have to wait for all content to load.
Read more… Read less…
window.onload
is the built-in JavaScript event, but as its implementation had subtle quirks across browsers (Firefox, Internet Explorer 6, Internet Explorer 8, and Opera), jQuery provides document.ready
, which abstracts those away, and fires as soon as the page's DOM is ready (doesn't wait for images, etc.).
$(document).ready
(note that it's not document.ready
, which is undefined) is a jQuery function, wrapping and providing consistency to the following events:
document.ondomcontentready
/document.ondomcontentloaded
- a newish event which fires when the document's DOM is loaded (which may be some time before the images, etc. are loaded); again, slightly different in Internet Explorer and in rest of the world- and
window.onload
(which is implemented even in old browsers), which fires when the entire page loads (images, styles, etc.)
$(document).ready()
is a jQuery event. JQuery’s $(document).ready()
method gets called as soon as the DOM is ready (which means that the browser has parsed the HTML and built the DOM tree). This allows you to run code as soon as the document is ready to be manipulated.
For example, if a browser supports the DOMContentLoaded event (as many non-IE browsers do), then it will fire on that event. (Note that the DOMContentLoaded event was only added to IE in IE9+.)
Two syntaxes can be used for this:
$( document ).ready(function() {
console.log( "ready!" );
});
Or the shorthand version:
$(function() {
console.log( "ready!" );
});
Main points for $(document).ready()
:
- It will not wait for the images to be loaded.
- Used to execute JavaScript when the DOM is completely loaded. Put event handlers here.
- Can be used multiple times.
- Replace
$
withjQuery
when you receive "$ is not defined." - Not used if you want to manipulate images. Use
$(window).load()
instead.
window.onload()
is a native JavaScript function. The window.onload()
event fires when all the content on your page has loaded, including the DOM (document object model), banner ads and images. Another difference between the two is that, while we can have more than one $(document).ready()
function, we can only have one onload
function.
A Windows load event fires when all the content on your page is fully loaded including the DOM (document object model) content and asynchronous JavaScript, frames and images. You can also use body onload=. Both are the same; window.onload = function(){}
and <body onload="func();">
are different ways of using the same event.
jQuery $document.ready
function event executes a bit earlier than window.onload
and is called once the DOM(Document object model) is loaded on your page. It will not wait for the images, frames to get fully load.
Taken from the following article:
how $document.ready()
is different from window.onload()
A little tip:
Always use the window.addEventListener
to add an event to window. Because that way you can execute the code in different event handlers .
Correct code:
window.addEventListener('load', function () {
alert('Hello!')
})
window.addEventListener('load', function () {
alert('Bye!')
})
Invalid code:
window.onload = function () {
alert('Hello!') // it will not work!!!
}
window.onload = function () {
alert('Bye!')
}
This is because onload is just property of the object, which is overwritten.
By analogy with addEventListener
, it is better to use $(document).ready()
rather than onload.
$(document).ready(function() {
// Executes when the HTML document is loaded and the DOM is ready
alert("Document is ready");
});
// .load() method deprecated from jQuery 1.8 onward
$(window).on("load", function() {
// Executes when complete page is fully loaded, including
// all frames, objects and images
alert("Window is loaded");
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.7.2/jquery.min.js"></script>