What is the difference between window.onload
, document.onready
and body.onload
?
Asked
Active
Viewed 7.6k times
74
-
5[This](http://stackoverflow.com/questions/191157/window-onload-vs-body-onload) will help you... Best of luck! :) – PHP Aug 13 '10 at 05:07
-
11Why was this marked as a duplicate? This doesn't even mention jQuery, while the other has it in the title (`ready` is jQuery only). – Ciro Santilli OurBigBook.com Jun 13 '14 at 09:51
-
4Aggree with Ciro. Far too many people on StackOverflow think that JavaScript === jQuery. – Seanonymous Aug 26 '15 at 18:31
-
4Fully agree with you guys. Voting to reopen the question. – Racil Hilan Sep 14 '15 at 15:08
-
Probably because having jQuery wrapped around it makes no difference to what the different events mean. – Quentin Sep 14 '15 at 15:12
1 Answers
67
window.onload
will wait until all assets have finished downloading, such as images and scripts.
DOM ready waits until you can access the DOM via the API.
As a side note, in this day and age, you ought to be using window.addEventListener('load', function() { }, false)
or attachEvent()
for older IEs.

alex
- 479,566
- 201
- 878
- 984
-
1I think the DOM ready functionality of jQuery would also wait for scripts to load before firing wouldn't it? – screenm0nkey Apr 06 '11 at 13:52
-
But it cannot work for Chrome, Safari and Opera. Is there an alternative of `window.onload` for those browsers? – william Dec 01 '11 at 02:27
-
3
-
-
What you mean by "in this day and age" is that it should be acceptable to defer script execution to onload on IE8 (since IE6-7 are more or less a thing of the past)? – Camilo Martin Oct 03 '12 at 17:35
-
@CamiloMartin I mean use the event handler systems besides `onevent` properties. – alex Dec 14 '12 at 11:27
-
@HiroProtagonist document.readyState and DOMContentLoaded are native to JavaScript – Christophe Feb 07 '13 at 17:55
-
Note that this won't let you know when the contents of an iFrame have loaded :) http://stackoverflow.com/questions/20572734/load-event-not-firing-when-iframe-is-loaded-in-chrome – Dean Rather Aug 04 '15 at 03:13