window.addEventListener('message')是否会覆盖其他侦听器? [英] Does window.addEventListener('message') overwrite other listeners?
问题描述
我有一些使用.postMessage()
与iframe通信的代码,这意味着它需要在message
上添加一个侦听器以从iframe接收通信.我为此使用了通常的代码:
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);
此代码在客户端页面上运行,该页面上还有很多其他内容:分析,社交按钮等.当我在processMessage
函数中添加console.log
以调试来自iframe的通信时,我注意到了,它从也使用.postMessage
的第三方插件中吸收了很多其他流量.
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
.
忽略它们不是问题,因为我正在寻找来自iframe的非常具体的消息,但是我想确保我不会覆盖任何应该从FB脚本中提取这些消息的侦听器,因此向前.我之前遇到过多个相互覆盖的多个window.onresize
事件的问题.消息的事件侦听器有问题吗?
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
不会覆盖现有的事件侦听器,它只是添加一个新的方法名,如方法名所示.必须使用removeEventListener
方法删除现有的侦听器.
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.
这篇关于window.addEventListener('message')是否会覆盖其他侦听器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!