سؤال

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?

هل كانت مفيدة؟

المحلول

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

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top