javascriptdomdomcontentloaded

Does Javascript load event listener takes images into account?


Does this

window.addEventListener("load", function(event) {
  console.log("DOM including images loaded?")
});

fire after only the DOM has been loaded or when everything, including images have been loaded?

Is this what I'm looking for: https://developer.mozilla.org/en-US/docs/Web/API/Window/load_event ?

If so, is this

window.addEventListener('load', (event) => {
});

or this

window.onload = (event) => {
};

the better practise?

Thx!


Solution

  • Everything is written clearly in the article that you've posted:

    The load event is fired when the whole page has loaded, including all dependent resources such as stylesheets and images

    And for the other question either is fine, but the advantage of addEventListener is that you can add more than one event listener to the load event. Read this post for more info - What is the difference between window.onload() and document.addEventListener('load', ..)?