GWT inject script element into the html file

后端 未结 4 784
予麋鹿
予麋鹿 2021-02-05 23:51

On my gwt project. i have a script that call the dictionary:


i

相关标签:
4条回答
  • 2021-02-05 23:58

    You could simply add a <script> element in your *.gwt.xml file.

    <script src='conf/iw_dictionary.js' />
    

    onModuleLoad will only be called once the script is loaded (as if you had it in your html page).

    0 讨论(0)
  • 2021-02-06 00:06

    Basically you inject the script element in your onModuleLoad():

        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);
    

    The browser will automatically load it as soon as it's injected.

    0 讨论(0)
  • 2021-02-06 00:13

    Use com.google.gwt.core.client.ScriptInjector, since it was created specifically for stuff like this

    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();
    
    0 讨论(0)
  • 2021-02-06 00:16

    The answers form jusio, Dom and Thomas Broyer are all valid here. In my particular case, I was looking to inject a series of polyfill scripts into GWT for some IE8 support we needed when running native JS code. The polyfill scripts needed to be available to the GWT iframe's window context - NOT the host page. To do that, using ScriptInjector was the correct approach as it attaches the script at that level. You can make ScriptInjector install the scripts to a host window by using setWindow(TOP_WINDOW). Adding scripts with the <script> tag in my *.gwt.xml file seemed to be attaching to the host window as did using @Dom's approach.

    0 讨论(0)
提交回复
热议问题