10

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?

Adi Mor
  • 2,145
  • 5
  • 25
  • 44
  • A similar question was also asked: [GWT: deferred loading of external JS resources](http://stackoverflow.com/q/7968831/320399) – blong Apr 09 '13 at 14:09

4 Answers4

15

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();
Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
jusio
  • 9,850
  • 1
  • 42
  • 57
  • `ScriptInjector` class is available in GWT_V2.7.0. If I have to inject a javascript file with GWT_V.1.x then Is it possible or not? – SK. Dec 15 '14 at 13:04
  • It is possible, but you'll have to use solutions from answers below. You can use answer provided by Dom (but you'll have to use older api for element creation) or Thomas Broyer answer (I'm not 100% sure if it was working for GWT 1.XX) – jusio Dec 15 '14 at 13:55
  • I'm implementing CSRF guard in my web application. I have done all the required configuration and I can see token getting generated/injected into the request header. Let me tell you application's architecture a bit, I have JSP pages which makes call to services using Ajax to get the data and display in the UI. My question here is "Can we protect service calls also with CSRF guard?" If it is possible then how can I do this because it is not going to CsrfGuardFilter.java class when I'm debugging the execution. Thanks for your help. – SK. Dec 16 '14 at 13:57
8

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.

Stefan Falk
  • 23,898
  • 50
  • 191
  • 378
Dominik Bucher
  • 1,509
  • 13
  • 16
  • 1
    Useful for those of us using 2.3 (ScriptInjector is introduced in 2.4) – Andy Smith Jul 19 '12 at 17:52
  • I'm implementing CSRF guard in my web application. I have done all the required configuration and I can see token getting generated/injected into the request header. Let me tell you application's architecture a bit, I have JSP pages which makes call to services using Ajax to get the data and display in the UI. My question here is "Can we protect service calls also with CSRF guard?" If it is possible then how can I do this because it is not going to CsrfGuardFilter.java class when I'm debugging the execution. Thanks for your help. – SK. Dec 16 '14 at 13:58
3

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

Thomas Broyer
  • 64,353
  • 7
  • 91
  • 164
  • As I understand from you that I have to put `` in `Main.gwt.xml` file. My question to you is, Will this work in gwt V1.XXXX too? – SK. Dec 16 '14 at 05:39
  • Yes. It however won't work (by default at least) starting with GWT 2.7, as the default linker is now `xsiframe` which doesn't support ` – Thomas Broyer Dec 16 '14 at 09:47
  • I'm implementing CSRF guard in my web application. I have done all the required configuration and I can see token getting generated/injected into the request header. Let me tell you application's architecture a bit, I have JSP pages which makes call to services using Ajax to get the data and display in the UI. My question here is "Can we protect service calls also with CSRF guard?" If it is possible then how can I do this because it is not going to CsrfGuardFilter.java class when I'm debugging the execution. Thanks for your help. – SK. Dec 16 '14 at 13:58
0

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.

Barrie
  • 1,444
  • 19
  • 26