2013-11-27 2 views
0

) (context.apply하는 생존하지 - 이것은 후세의 원래 질문은-파이어 폭스 - 자바 스크립트 - window.event를 패스가

나는 코드의 새로운 조각과 약간의 문제가 있어요 나는 썼다. 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); 

작품 좋은, 그러나 파이어 폭스는 처음 frame 때마다 일치합니다. (어떤 window에 관계없이 iframe.contentWindow === window). 나는 원래 실제로 다른 게시판 here에서이 작업을 수행하는 방법을 찾았지만 Firefox에서 문제는 언급하지 않았습니다.

각 iframe마다 다른 src가 있습니다.

일치하는 다른 방법이 있습니까? 나는 명백하게 틀린 무엇인가 했느냐?

아니요 jQuery.

감사

더 나은 질문 :

내 이벤트가에 적용된 기능에서 사용할 수 window.event를 기대, window 객체를 통해 function.apply(window, params)를 통과했다 -이 크롬에서 작동/사파리, FF에있는 사건이 아니라고 생각 했어. FF로 이벤트를 전달하려면 어떻게합니까?

+0

iframe이 교차 출격입니까? src를 직접 확인할 수없는 이유는 무엇입니까? –

+0

@ReubenMorais 예, 그들은 교차 출처입니다. –

+0

방금 ​​시도해 보았지만 내가 말할 수있는 한 제대로 작동합니다. 문제가있는 전체 페이지로 연결할 수 있습니까? –

답변

0
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); 
관련 문제