body.onload 和 GWT onModuleLoad 启动顺序

发布于 2024-12-14 02:44:22 字数 1476 浏览 2 评论 0原文

根据此常见问题解答,当 GWT bootstraps 中,onModuleLoad 应该在 HTML body 的 onload 事件之前运行。该常见问题解答中详细说明的过程是这样的:

1. The HTML document is fetched and parsing begins.
...
9. externalScriptOne.js completes. The document is ready, so onModuleLoad() fires.
...
12. body.onload() fires, in this case showing an alert() box.

但在我的测试中,我已经检查它不会以这种方式工作。或者至少不是在每个浏览器中(奇怪的是,谷歌浏览器尤其不坚持这种行为)。例如,我有一个涉及 onModuleLoad 和 body.onLoad 的小测试:

public void onModuleLoad() {

    runTestFunction();

}

private native void runTestFunction() /*-{
    console.log("GWT's onModuleLoad");
    $wnd.loaded=true;
}-*/;

并且:

<body onload="console.log('body.onLoad');if(loaded!=null) console.log('loaded var is set');">

如果我启动 Firefox,并运行此示例,控制台将显示以下内容:

GWT's onModuleLoad
body.onLoad
loaded var is set

但在 Chrome 中:

body.onLoad
Uncaught ReferenceError: loaded is not defined
GWT's onModuleLoad

在后者中, onModuleLoad 最后运行,因此“已加载” var 尚不可用,body.onLoad 代码无法使用它。

我想要实现什么目标?我想要一些在 body.onload 中运行的手写 Javascript 来与我的 GWT 代码交互。在此示例中,我使用这个虚拟“加载”var,但将来它应该能够调用用 Java 编写的 GWT 函数。问题是我需要确保 onModuleLoad 首先运行,以便它可以导出 JavaScript 访问它们的变量和方法。

那么,我错过了什么?这种行为是否像看起来那样不可靠,或者我做错了什么?

PS:我有一个 B 计划来实现这一点,该计划已被证明是有效的,但首先我想确保不可能这样做,因为这应该是首选方法。

According to this FAQ, when GWT bootstraps, onModuleLoad is supossed to run before HTML body's onload event. The process detailed within that FAQ works like this:

1. The HTML document is fetched and parsing begins.
...
9. externalScriptOne.js completes. The document is ready, so onModuleLoad() fires.
...
12. body.onload() fires, in this case showing an alert() box.

But in my tests, i have checked that it doesnt work this way. Or at least not in every browser (oddly, Google Chrome in particular doesn't stick to this kind of behaviour). For example, I have this little test involving onModuleLoad and body.onLoad:

public void onModuleLoad() {

    runTestFunction();

}

private native void runTestFunction() /*-{
    console.log("GWT's onModuleLoad");
    $wnd.loaded=true;
}-*/;

And:

<body onload="console.log('body.onLoad');if(loaded!=null) console.log('loaded var is set');">

If i launch firefox, and run this example, the console will show this:

GWT's onModuleLoad
body.onLoad
loaded var is set

But in Chrome:

body.onLoad
Uncaught ReferenceError: loaded is not defined
GWT's onModuleLoad

In the latter, onModuleLoad runs the last, thus "loaded" var is not yet available and body.onLoad code cant use it.

And what Im trying to achieve? I want some handwritten Javascript that runs within body.onload to interact with my GWT code. In this example i use this dummy "loaded" var, but in the future it should be able to call GWT functions written in Java. The problem is that i need to make sure that onModuleLoad runs first so it can export the variables and methods for javascript to access them.

So, what am i missing? Is this behaviour as unreliable as it looks like, or am i doing something wrong?

PS: i have a plan B to achieve this which is proved to work, but first i want to make sure that it isnt possible to do it this way since this should be the preferred method.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

悲歌长辞 2024-12-21 02:44:22

首先,该文档的最新版本位于 http://code.google .com/webtoolkit/doc/latest/DevGuideOrganizingProjects.html#DevGuideBootstrap

它说(甚至是您正在寻找的 GWT 1.5 版本) at) “onModuleLoad() 可以在解析外部文档后的任何时候调用”,其中包括 window.onload 之前和之后。

正如文档所说,GWT 将代码加载到 iframe(此处用作沙箱)中,这是异步的;因此,当 iframe 和“body”都加载时,您的代码就会加载。根据加载 iframe 所需的时间,可以在 window.onload 之前或之后(在示例中,他们假设它立即加载,当 *. cache.* 文件实际上位于浏览器的缓存中)。

但根据经验,GWT 会尽力(至少是内置链接器)使事情异步启动,这样就不会中断其他外部资源(例如样式表和图像)的加载。这意味着不能保证它在 window.onload 之前运行(他们可以保证在 window.onload 之后运行,但为什么还要等待?)

First, the latest version of the doc is at http://code.google.com/webtoolkit/doc/latest/DevGuideOrganizingProjects.html#DevGuideBootstrap

And it says (even the GWT 1.5 version you were looking at) that "onModuleLoad() can be called at any point after the outer document has been parsed", which includes before and after window.onload.

As the doc says, GWT loads your code in an iframe (used here as a sandbox), which is asynchronous; so your code loads when both the iframe and the "body" are loaded. Depending on the time needed to load the iframe, that can be before or after window.onload (in the example, they assume it loads right away, which could be the case when the *.cache.* file is effectively in the browser's cache).

But the rule of thumb is that GWT tries hard (at least the built-in linkers) to make things start asynchronously so that it doesn't break loading of other external resources (stylesheets and images, for instance). That implies that it cannot be guaranteed to run before the window.onload (they could have guaranteed to run after window.onload, but why wait?)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文