Firefox-Javascript-window.event无法幸存并传递给context.apply() [英] Firefox - Javascript - window.event not surviving pass to context.apply()

查看:78
本文介绍了Firefox-Javascript-window.event无法幸存并传递给context.apply()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

-这是后代的原始问题-

-This is the original question for posterity-

我编写的新代码有些麻烦.我正在尝试查找发送消息的窗口所在的iframe,假设它完全来自iframe.相关代码:

I'm having a bit of trouble with a new piece of code I wrote. I'm trying to find the iframe the window that sent a message lives in, assuming it's from an iframe at all. The relevant code:

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);

在Chrome/Safari中工作正常,但是Firefox每次都匹配第一个frame. (iframe.contentWindow === window,而不管哪个window).实际上,我最初在另一篇此处中找到了解决方法,尽管他们没有提到Firefox的问题.

Works fine in Chrome/Safari, however Firefox matches the first frame every time. (iframe.contentWindow === window regardless of which window). I actually originally found how to do this from another post here, though they don't mention the problem with Firefox.

每个iframe都有不同的src.

Each iframe has a different src.

是否有其他匹配这些方法的方法?我做错了什么明显的事吗?

Is there a different method for matching these? Have I done something obviously wrong?

请不要使用jQuery.

谢谢

更好的问题:

我的事件正在通过function.apply(window, params)通过窗口对象传递,希望它所应用的函数中有window.event可用-这在Chrome/Safari中有效,认为在FF中不是这种情况.如何获取要在FF中传递的事件?

My event was being passed through a function.apply(window, params) through the window object, expecting window.event available in the function it was applied to - this works in Chrome/Safari, thought not the case in FF. How do I get the event to be passed in FF?

推荐答案

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);

这篇关于Firefox-Javascript-window.event无法幸存并传递给context.apply()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆