user2894386
user2894386

Reputation: 69

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?

Upvotes: 3

Views: 1228

Answers (3)

Weilory
Weilory

Reputation: 3101

  1. window.addEventListener('load', function(){}) is allowed for multiple times

  2. window.onload will overwrite all load listeners.

  3. 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.

Upvotes: 0

Paul Draper
Paul Draper

Reputation: 83215

It is allowed that is the difference between addEventListener and window.onload =.

The first can have multiple listeners. The latter, only one.

Upvotes: 2

bfavaretto
bfavaretto

Reputation: 71908

Yes, you can. In fact, that's one of the biggest advantages of addEventListener versus a plain window.onload = function....

Upvotes: 2

Related Questions