[whatwg] using postMessage() to send to a newly-created window
Dirk Pranke
dpranke at google.com
Tue Apr 6 17:26:14 PDT 2010
Sorry for the delay in replying ...
On Thu, Mar 25, 2010 at 1:31 AM, Ian Hickson <ian at hixie.ch> wrote:
> On Mon, 21 Dec 2009, Dirk Pranke wrote:
>>
>> In the course of testing something today, I attempted to create a window
>> and immediately post a message to it, and was surprised that it didn't
>> seem to work.
>>
>> E.g.:
>>
>> var w = window.open("http://x");
>> w.postMessage("hello, world", "*");
>>
>> w never got the message - this seemed to be consistent across Safari,
>> Chrome, and FF (all I had installed on my Mac at the time, so
>> apologies to Opera, IE, and anyone else I've left out).
>
> It got the message, it just got it when the window's document was
> about:blank and before the scripts in that window were ready to receive
> any messages.
>
>
>> Is this supposed to work? If not, is there a reliable way for the the
>> source to know when it is safe to send a message to the target?
>
> If it's same-origin, the simplest solution is to wait for onload.
>
> If it's cross-origin, there is by design no mechanism to expose when the
> page is ready short of the other site announcing it (by sending a message
> to its opener, in this case).
>
>
>> If this isn't supposed to work, can we state this explicitly in the
>> spec?
>
> Where would you like it mentioned and what would you like it to say?
>
How about putting some text at the end of Section 9.2.3, "Posting
Messages" that says something like:
"Note that messages are not guaranteed to be delivered reliably under
all circumstances. User agents are allowed to not deliver messages if
the details of their implementation may make this difficult or
impossible to do safely and securely. For example, if a new child
window is created and then a message is immediately posted to it, that
message may not get delivered, even if the child window belongs to the
same origin. A rationale for this may me that it would require the
user agent to queue the message across creation of a new event loop,
leading to all sorts of unpredictable timing issues."
I'm not particularly happy with the wording here, but it gets the
point across. Of course, putting in text like this would naturally
lead the reader to wonder under what conditions messages *are*
guaranteed to be delivered reliably. Which is, I think, a valid
question I don't know the answer to. Is it safe to say that messages
are delivered reliably if the destination window belongs to the
specified targetOrigin and the destination window is fully loaded?
What if a navigation event is triggered on the destination window?
-- Dirk
More information about the whatwg
mailing list