Home‎ > ‎UI‎ > ‎

Script Injection


Basic Javascript 
private void drawTwitter() {
   
String s = "<a href=\"http://twitter.com/Gawkat\" class=\"twitter-follow-button\" data-show-count=\"false\">Follow @Gawkat</a>";
    HTML h
= new HTML(s);
    fpTwitter
.add(h);
   
   
Document doc = Document.get();
   
ScriptElement script = doc.createScriptElement();
    script
.setSrc("http://platform.twitter.com/widgets.js");
    script
.setType("text/javascript");
    script
.setLang("javascript");
    doc
.getBody().appendChild(script);
 
}

Javascript Injection With a Callback
public class LoadApi {
 
/**
   * load api
   *
   * @param onLoad - callback on load of api success
   * @param onLoadFail - callback on load api failure
   * @param sensor - derive location [true|false]
   */

 
public static void go(Runnable onLoad, Runnable onLoadFail, boolean sensor) {
    load
(onLoad, onLoadFail, sensor);
 
}
 
 
private static void load(final Runnable onLoad, final Runnable onLoadFail, boolean sensor) {
   
ScriptInjector.fromUrl("http://maps.googleapis.com/maps/api/js?sensor=" + sensor).setCallback(
       
new Callback<Void, Exception>() {
         
public void onFailure(Exception reason) {
            onLoadFail
.run();
         
}
         
public void onSuccess(Void result) {
            onLoad
.run();
         
}
       
}).inject();
 
}
 
 
/**
   * private constructor
   */

 
private LoadApi() {
 
}
}






Comments