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

26,526

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

Share:
26,526
Chris Wilson
Author by

Chris Wilson

Author of RaphaelJS: Graphics and Visualization on the Web Director of Data Journalism, Time.com https://mechanicalscribe.com/ Twitter: @chriswilsondc GitHub: @wilson428 LinkedIn

Updated on March 06, 2020

Comments

  • Chris Wilson
    Chris Wilson about 4 years

    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?