Class ScriptInjector

java.lang.Object
com.google.gwt.core.client.ScriptInjector

public class ScriptInjector extends Object
Dynamically create a script tag and attach it to the DOM. Usage with script as local string:

   String scriptBody = "var foo = ...";
   ScriptInjector.fromString(scriptBody).inject();
 

Usage with script loaded as URL:

   ScriptInjector.fromUrl("http://example.com/foo.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();
 
  • Field Details

    • TOP_WINDOW

      public static final JavaScriptObject TOP_WINDOW
      Returns the top level window object. Use this to inject a script so that global variable references are available under $wnd in JSNI access.

      Note that if your GWT app is loaded from a different domain than the top window, you may not be able to add a script element to the top window.

  • Method Details

    • fromString

      public static ScriptInjector.FromString fromString(String scriptBody)
      Build an injection call for directly setting the script text in the DOM.
      Parameters:
      scriptBody - the script text to be injected and immediately executed.
    • fromUrl

      public static ScriptInjector.FromUrl fromUrl(String scriptUrl)
      Build an injection call for adding a script by URL.
      Parameters:
      scriptUrl - URL of the JavaScript to be injected.