Re: connection ceremony for iframe postMessage communications

On Thu, Feb 9, 2012 at 11:49 AM, Boris Zbarsky <bzbarsky@mit.edu> wrote:
> On 2/9/12 1:15 PM, John J Barton wrote:
>>
>> This leads developers to look for events that will tell them about
>> 'load' on iframes, and that leads them to try
>> iframe.contentWindow.addEventListener(). It works fine for same-domain
>> iframes, but fails for cross-domain.
>
>
> Adding a load listener to the iframe element itself should work for this,
> no?

I guess you mean: by issuing
  iframe.addEventListener('load', handler, false);
you get notified when the iframe load event has completed (but you
don't need to touch the contentWindow property).

This will work if the iframe ensures that it completes its connection
work before 'load'. This prevents the iframe from using async loading
for the scripts that create the connection and for any code that
handles messages from the parent. Which, in a typical iframe
component, would be all the code, since its main job is to provide for
the parent.

In addition this solution requires that the above addEventListener be
attached after the iframe is inserted (so the iframe exists) but
before the parent's 'load' event (which is after the iframe load and
thus too late.

So I'd say it does not solve the original problem and it's hard to use
too. Other than that it's a fine idea ;-).

jjb

Received on Thursday, 9 February 2012 20:18:04 UTC