GWT:在页面准备好之前开始运行(或者身体资源不阻止运行gwt)?

Ali*_*iba 5 javascript gwt

在我的html页面中有外部图像和闪烁,有时它们加载缓慢.我的gwt应用程序总是在加载后开始运行.我使用xsiframe链接器.

在加载体内的任何图像和资源之前,有没有办法开始运行gwt?或者确保加载其他东西不会阻止启动gwt.

编辑:我创建了一个重现问题的最小项目:

入口点:

public class MyEntryPoint implements EntryPoint {
    public void onModuleLoad() {
        Window.confirm("onModuleLoad");
    }
}
Run Code Online (Sandbox Code Playgroud)

html页面:

<html><head>
    <script type="text/javascript"src="nocache.js"></script>
</head><body>
    <!-- http://stackoverflow.com/questions/100841/artificially-create-a-connection-timeout-error -->
    <img src="http://10.255.255.1/timeout.gif" alt="timeout image" />
</body></html>
Run Code Online (Sandbox Code Playgroud)

Chr*_*her 1

我能想到两种可能的解决方案:

1.xs链接器

您可以使用旧的“xs”链接器而不是“xsiframe”。这不太好,特别是因为 xs 不能在开发模式下工作。但也许您可以在开发期间使用 xsiframe,然后切换到 xs 进行真正的构建、测试和生产。

2.<noscript>标签技巧

您可以将页面内容,尤其是缓慢加载的图像,放入标签中<noscript>,如果浏览器不支持 JavaScript,则可以使用该标签。

但是,如果启用了 JavaScript,则加载过程中该标记的内容将被忽略。因此,我们将在 GWT 代码(可以再次使用 xsiframe 链接器)中执行的操作是将 noscript 标记的内容复制回真实页面。

这是一些快速代码:

<!doctype html>

<html>
  <head>
    <meta http-equiv="content-type" content="text/html; charset=UTF-8">

    <script type="text/javascript" language="javascript"
            src="http://cdn.example.org/.../some.nocache.js"></script>
  </head>

  <body>

    <noscript>
      <p>A</p>
      <img src="http://10.255.255.1/timeout.gif" alt="timeout image" />
      <p>B</p>
    </noscript>

    <p>Test (should appear immediately if JavaScript is enabled)</p>
  </body>
</html>
Run Code Online (Sandbox Code Playgroud)
@Override
public void onModuleLoad() {

  final NodeList<Element> noscriptElems = 
      RootPanel.get().getElement().getElementsByTagName("noscript");

  final Element noscriptElem = noscriptElems.getItem(0);

  RootPanel.get().add(new Label("Here is GWT"));

  RootPanel.get().add(new HTMLPanel(noscriptElem.getInnerText()));
      /* Be careful of course, not to have bad html in your noscript elem */

}
Run Code Online (Sandbox Code Playgroud)