从外部脚本向 GWT 父级发送消息
我刚刚发现了使用 html5 的 postMessage 从一个 iframe 到另一个 iframe 进行通信的乐趣。
但是,我遇到的问题是我正在运行一个加载 iframe(具有外部域)的 GWT 应用程序。我现在想要从该外部域将消息发回 GWT 应用程序。我天真地首先尝试:
parent.postMessage('hello', '*');
从外部脚本,同时在我的 GWT 应用程序中监听消息事件。这不起作用,因为 GWT 的 javascript 在 iframe 中自行运行。
我让它工作的方法是:
document.getElementById(myGwtModuleName).postMessage('hello', '*');
很好,它正在工作,但是如果我决定更改我的 Gwt 模块名称怎么办?整个过程会中断,一年后可能需要花很多时间才能弄清楚原因。
是否有更好的方法将消息发回 GWT 应用程序?或者我怎样才能在运行时找出 Gwt 的模块名称?在这种情况下,我可以将它作为参数传递给 iframe。
感谢您的帮助!
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
遇到了同样的问题,花了我一段时间,但你必须调用它,才能使其正常工作:)
这是解决方案
问候,
斯特凡
had the same problem and took me while but you have to call this, to get it working :)
here is the solution
Regards,
Stefan
如何将侦听器添加到当前窗口(例如包含 GWT 脚本的窗口)。
您应该使用 $doc 或 $wnd 变量作为侦听器的目标。 $doc 变量始终是对包含 GWT 应用程序引导脚本的文档的引用。
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.