Skip to content Skip to sidebar Skip to footer

What Is The Difference Between Window.onload() And Document.addeventlistener('load', ..)?

It seems to me that both events are fired when every resource and its dependent resources have finished loading. This rises some questions: Is there any difference between those e

Solution 1:

  1. As you say, they both do exactly the same thing. The advantage of addEventListener is that you can add more than one event listener to the load event.

  2. From some basic testing, it seems the listeners get called in the order they were set, though I don't know how reliable that is.

  3. You can use either method to do whatever you need.

Solution 2:

They do NOT do exactly the same thing, at least in Firefox.

The reason is that window.onload is equivalent to window.addEventListener("load"), not document.addEventListener("load").

Although, all documentation I have seen says that they are equivalent.

Solution 3:

1- From what I know adding an event listening gives you more freedom especially in removing the controls you added

2- Nothing I know about that. But I assume it shouldn't be a big difference

3- The EventListener allows you to control the element and remove the control at will and thus should be more useful

Post a Comment for "What Is The Difference Between Window.onload() And Document.addeventlistener('load', ..)?"