GWT将脚本元素注入到html文件中

Adi*_*Mor 10 gwt

在我的gwt项目上.我有一个调用字典的脚本:

<script type="text/javascript" src=conf/iw_dictionary.js></script>
Run Code Online (Sandbox Code Playgroud)

而不是在html文件中编写此脚本元素.我想从入口点,在模块加载上将它注入到html中.

我该怎么做?

jus*_*sio 13

使用com.google.gwt.core.client.ScriptInjector,因为它是专门为这样的东西创建的

ScriptInjector.fromUrl("conf/iw_dictionary.js").setCallback(
  new Callback<Void, Exception>() {
     public void onFailure(Exception reason) {
       Window.alert("Script load failed.");
     }
    public void onSuccess(Void result) {
      Window.alert("Script load success.");
     }
  }).inject();
Run Code Online (Sandbox Code Playgroud)


Dom*_*her 7

基本上你在onModuleLoad()中注入了script元素:

    Element head = Document.get().getElementsByTagName("head").getItem(0);
    ScriptElement sce = Document.get().createScriptElement();
    sce.setType("text/javascript");
    sce.setSrc("conf/iw_dictionary.js");
    head.appendChild(sce);
Run Code Online (Sandbox Code Playgroud)

浏览器会在注入后立即自动加载.