Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between load vs DOMContentLoaded [duplicate]

Tags:

javascript

Possible Duplicate:
Difference between DOMContentLoaded and Load events

Whats the difference between

window.addEventListener("load", load, false); 

and

document.addEventListener("DOMContentLoaded", load, false); 

?

like image 576
Sudhir Avatar asked Jan 12 '12 12:01

Sudhir


People also ask

What is the difference between DOMContentLoaded and load?

The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images. This is in contrast to DOMContentLoaded , which is fired as soon as the page DOM has been loaded, without waiting for resources to finish loading. This event is not cancelable and does not bubble.

When should I use DOMContentLoaded?

The DOMContentLoaded event is a useful event that can make a big impact on the performance of your pages, hence this example. It fires when the HTML and scripts that make up the page have loaded, but not necessarily the images or CSS.

What does DOMContentLoaded mean?

The DOMContentLoaded event fires when the initial HTML document has been completely loaded and parsed, without waiting for stylesheets, images, and subframes to finish loading. A different event, load , should be used only to detect a fully-loaded page.

What happens after DOMContentLoaded?

The DOMContentLoaded event fires when the HTML document has been completely parsed, and all deferred scripts ( <script defer src="…"> and <script type="module"> ) have downloaded and executed. It doesn't wait for other things like images, subframes, and async scripts to finish loading.


2 Answers

  • DOMContentLoaded - the whole document (HTML) has been loaded.
  • load - the whole document and its resources (e.g. images, iframes, scripts) have been loaded.
like image 147
Krizz Avatar answered Oct 14 '22 10:10

Krizz


DOMContentLoaded awaits only for HTML and scripts to be loaded.
window.onload and iframe.onload triggers when the page is fully loaded with all dependent resources including images and styles.

Here is more details you can find http://javascript.info/tutorial/onload-ondomcontentloaded

like image 21
Vadim Gulyakin Avatar answered Oct 14 '22 10:10

Vadim Gulyakin