'Javascript more than 1 window.addeventlistener with "load" allowed?
Is it allowed to have more than one window.addEventlistener("load", function, false);?
Like this:
window.addEventlistener("load", function1, false);
window.addEventlistener("load", function2, false);
I know you don't have to, but is it wrong? Or is it allowed?
Solution 1:[1]
Yes, you can. In fact, that's one of the biggest advantages of addEventListener versus a plain window.onload = function....
Solution 2:[2]
It is allowed that is the difference between addEventListener and window.onload =.
The first can have multiple listeners. The latter, only one.
Solution 3:[3]
window.addEventListener('load', function(){})is allowed for multiple timeswindow.onloadwill overwrite all load listeners.window.addEventListener('load', function(){window.addEventListener('load', function(){})})you should not use load event listener as IIFE, since than any more load listener you added nested-ly, will not execute.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 | bfavaretto |
| Solution 2 | Paul Draper |
| Solution 3 | Weilory |
