Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

GWT inject script element into the html file

Tags:

gwt

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

<script type="text/javascript" src=conf/iw_dictionary.js></script>

instead of writing this script element in the html file. i want to inject it into the html from the entry point, on the module load.

how can i do it?

like image 557
Adi Mor Avatar asked Jan 29 '12 08:01

Adi Mor


3 Answers

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();
like image 91
jusio Avatar answered Oct 22 '22 02:10

jusio


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.

like image 35
Dominik Bucher Avatar answered Oct 22 '22 03:10

Dominik Bucher


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

like image 24
Thomas Broyer Avatar answered Oct 22 '22 04:10

Thomas Broyer