Search code examples
javascriptgwtiframepostmessage

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!


Solution

  • 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