Rene
Rene

Reputation: 33

postMessage from external script to GWT parent

I just found the joy of communicating from one iframe to another using html5's postMessage.

However, the problem I'm having is that I'm running a GWT application that loads the iframe (with external domain). From that external domain I now want to post a message back to the GWT application. Naively I first tried:

parent.postMessage('hello', '*');

from the external script, while listening to message events in my GWT application. This doesn't work because GWT's javascript runs itself in an iframe.

The way I got it working is by doing:

document.getElementById(myGwtModuleName).postMessage('hello', '*');

Great that it's working but what if I would decide to change my Gwt's module name? The whole process would break and in a year it would probably take a lot of head scratching to figure out why..

Is there a better way to post a message back to the GWT application? Or alternatively how can i figure out what Gwt's module name is at runtime? In that case I could pass it as a parameter to the iframe.

Thanks for any help!

Upvotes: 3

Views: 2405

Answers (2)

Stefan
Stefan

Reputation: 14863

had the same problem and took me while but you have to call this, to get it working :)

here is the solution

    public final native void doPost() /*-{
    $wnd.parent.postMessage("Hello parent from your GWT iFrame!", '*');
}-*/;

Regards, Stefan

Upvotes: 5

jusio
jusio

Reputation: 9920

How do you add listeners to the current window (e.g. window where GWT script is included). You should use $doc or $wnd variables, as target for listeners. $doc variable is always a reference to the document which contains bootstrap script for GWT application.

Upvotes: 0

Related Questions