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

大兔子大兔子 提交于 2019-12-21 04:01:21

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/22204902/does-window-addeventlistenermessage-overwrite-other-listeners

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!