簡體   English   中英

Firefox-Javascript-window.event無法幸存並傳遞給context.apply()

[英]Firefox - Javascript - window.event not surviving pass to context.apply()

- 這是后代的原始問題 -

我編寫的新代碼有些麻煩。 我正在嘗試查找發送消息的窗口所在的iframe,假設它完全來自iframe。 相關代碼:

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的問題。

每個iframe都有一個不同的src。

是否有其他匹配這些方法的方法? 我做錯了什么嗎?

請不要jQuery。

謝謝

更好的問題:

我的事件正在通過window對象的function.apply(window, params)傳遞,期望window.event在它所應用的函數中可用-這在Chrome / Safari中有效,但在FF中不適用。 如何獲得要在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);

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM