0

-This is the original question for posterity-

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

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.

Each iframe has a different src.

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

No jQuery, please.

Thanks

Better Question:

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?

Community
  • 1
  • 1
Randy Hall
  • 7,716
  • 16
  • 73
  • 151
  • Are the iframes cross-origin? Any reason why you can't check the src directly? – Reuben Morais Nov 27 '13 at 19:06
  • @ReubenMorais yes, they are cross-origin. – Randy Hall Nov 27 '13 at 19:23
  • I just tried this, and it works fine as far as I can tell. Can you link to a complete page that shows the problem for you? – Boris Zbarsky Nov 27 '13 at 20:00
  • @BorisZbarsky As it is on an internal server behind layers of security, I can not. I'm working on getting a more robust example working in a fiddle. Though further testing shows that the reference to the event-dispatching-window may be lost altogether... I am passing this through other functions, the example above is just the highlights. More soon. – Randy Hall Nov 27 '13 at 20:06
  • Cannot reproduce: http://jsfiddle.net/P9bHr/1 – Rob W Nov 27 '13 at 21:46
  • @BorisZbarsky Alright it looks like something else I was doing caused this. The event object on the window/context while passing said context into function.apply(context, params) (not shown above) wasn't surviving in FF. I was manually setting up an event object to account for this, which I wasn't clearing, ever, causing the mixup - the reference to the event object was floating around from an earlier message to the page. I'll clear up the question. – Randy Hall Dec 02 '13 at 13:24

1 Answers1

1
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);
Randy Hall
  • 7,716
  • 16
  • 73
  • 151