Firefox-Javascript-window.event无法传递到context.apply()
-这是留给后代的原始问题- 我写的一段新代码有点麻烦。我试图找到发送消息的窗口所在的iframe,假设它来自iframe。有关守则:Firefox-Javascript-window.event无法传递到context.apply(),javascript,firefox,iframe,window,postmessage,Javascript,Firefox,Iframe,Window,Postmessage,-这是留给后代的原始问题- 我写的一段新代码有点麻烦。我试图找到发送消息的窗口所在的iframe,假设它来自iframe。有关守则: var getContainingFrame = function(source){ var frames = document.getElementsByTagName('iframe'); for (var i = 0; i < frames.length; ++i) { if (frames[i].contentWind
var getContainingFrame = function(source){
var frames = document.getElementsByTagName('iframe');
for (var i = 0; i < frames.length; ++i) {
if (frames[i].contentWindow === source) {
return frames[i];
}
}
return false;
}
var postMessageReceivedCallback = function(event){
getContainingFrame(event.source);
}
window.addEventListener("message", postMessageReceivedCallback, false);
var getContainingFrame=函数(源){
var frames=document.getElementsByTagName('iframe');
对于(变量i=0;i
在Chrome/Safari中运行良好,但是Firefox每次都匹配第一个帧。(iframe.contentWindow===window
无论哪个窗口
)。事实上,我最初是在另一篇文章中发现如何做到这一点的,尽管他们没有提到Firefox的问题
每个iframe都有一个不同的src
是否有不同的方法来匹配这些?我做错什么了吗
请不要用jQuery。
谢谢
更好的问题:
我的事件是通过一个函数传递的。apply(window,params)
通过window对象传递的,期望window.event在它所应用的函数中可用-这在Chrome/Safari中有效,但在FF中不是这样。如何在FF中传递事件?iFrame是否跨原点?有什么原因不能直接检查src吗?@ReubenMorais是的,它们是交叉源代码。我刚刚试过这个,据我所知,它工作得很好。你能链接到一个完整的页面,为你显示问题吗?@BorisZbarsky因为它位于一个内部服务器的安全层后面,我不能。我正在努力得到一个更健壮的小提琴示例。尽管进一步的测试表明,对事件调度窗口的引用可能会完全丢失。。。我将通过其他函数传递此信息,上面的示例只是其中的亮点。很快。@RandyHall好的,让我知道!
var someFunction = function(params){
this.event.source //expect this to be the window the event originated in
//... do something, like the iframe check
}
var postMessageReceivedCallback = function(event){
//FF not keeping the event reference on the window that other browsers are,
//so we add it ourselves from the event object passed by the message event.
window.event = event;
someFunction.apply(window, event.message);
}
window.addEventListener("message", postMessageReceivedCallback, false);