11

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?

1

4 Answers 4

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();
Sign up to request clarification or add additional context in comments.

3 Comments

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

2 Comments

Useful for those of us using 2.3 (ScriptInjector is introduced in 2.4)
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.
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).

3 Comments

As I understand from you that I have to put <script src='conf/iw_dictionary.js' /> in Main.gwt.xml file. My question to you is, Will this work in gwt V1.XXXX too?
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 <script> in module files (you then have to use ScriptInjector or include the script in your HTML host page instead).
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.
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.