Ren*_*ene 3 javascript iframe gwt postmessage
我刚刚发现了使用 html5 的 postMessage 从一个 iframe 到另一个 iframe 进行通信的乐趣。
但是,我遇到的问题是我正在运行一个加载 iframe(具有外部域)的 GWT 应用程序。我现在想从该外部域将消息发回 GWT 应用程序。我天真地第一次尝试:
parent.postMessage('hello', '*');
Run Code Online (Sandbox Code Playgroud)
从外部脚本,同时侦听我的 GWT 应用程序中的消息事件。这不起作用,因为 GWT 的 javascript 在 iframe 中自行运行。
我让它发挥作用的方法是:
document.getElementById(myGwtModuleName).postMessage('hello', '*');
Run Code Online (Sandbox Code Playgroud)
很高兴它能正常工作,但是如果我决定更改 Gwt 的模块名称怎么办?整个过程可能会中断,一年之内可能需要花很多时间才能弄清楚原因。
是否有更好的方法将消息发回 GWT 应用程序?或者我如何在运行时找出 Gwt 的模块名称是什么?在这种情况下,我可以将它作为参数传递给 iframe。
谢谢你的帮助!
遇到了同样的问题,花了我一段时间,但你必须调用它,才能使其正常工作:)
这是解决方案
public final native void doPost() /*-{
$wnd.parent.postMessage("Hello parent from your GWT iFrame!", '*');
}-*/;
Run Code Online (Sandbox Code Playgroud)
问候,斯特凡