Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Does window.addEventListener('message') overwrite other listeners? [duplicate]

I've got some code that communicates with an iframe using .postMessage(), meaning it needs to add a listener on message to receive communication from the iframe. I'm using the usual code for that:

window.addEventListener('message', processMessage, false);

This code runs on a client's page that has a bunch of other stuff on it: Analytics, social buttons, etc. etc. I noticed when I added a console.log to the processMessage function to debug communication from the iframe, it was picking up a lot of other traffic from third-party plugins that also use .postMessage.

It's not a problem to ignore them, since I'm looking for very specific messages from the iframe, but I want to make sure I'm not overwriting whatever listener was supposed to pick up those messages from the FB script and so forth. I've had issues before with multiple window.onresize events overwriting one another. Is that an issue with the event listener for messages?

like image 657
Chris Wilson Avatar asked Mar 05 '14 17:03

Chris Wilson


1 Answers

addEventListener does not overwrite existing event listeners, it simply adds a new one as the method name implies. Existing listeners must be removed using the removeEventListener method.

addEventListener info

removeEventListener info

like image 152
Will P. Avatar answered Sep 18 '22 14:09

Will P.