Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

window.onload vs. body.onload vs. document.onready [duplicate]

Tags:

javascript

What is the difference between window.onload, document.onready and body.onload ?

like image 573
alter Avatar asked Aug 13 '10 05:08

alter


People also ask

What is the difference between window onload and document onload?

The general idea is that window. onload fires when the document's window is ready for presentation and document. onload fires when the DOM tree (built from the markup code within the document) is completed.

Is there any difference between body onload () and document ready () function?

The main differences between the two are: Body. Onload() event will be called only after the DOM and associated resources like images got loaded, but jQuery's document. ready() event will be called once the DOM is loaded i.e., it wont wait for the resources like images to get loaded.

What is difference between document and window Load method?

The key difference between $(document). ready() and $(window). load() event is that the code included inside onload function will run once the entire page(images, iframes, stylesheets,etc) are loaded whereas the $(document). ready() event fires before all images,iframes etc.

Can you have multiple window onload?

Unfortunately, you cannot place multiple onload events on a single page. You can nest multiple functions within the one onload call, but what if you need to cascade an onload script across multiple pages, some which may already have an existing onload event? use the addLoadEvent function below.


1 Answers

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.

like image 80
alex Avatar answered Oct 16 '22 16:10

alex