1 Reply Latest reply on Jun 24, 2014 3:35 AM by Aaron Anderson

    Inline script elements in template files

    Aaron Anderson Newbie

      I have some pages that utilize page specific inline scripts to initialize JavaScript variables for some JQuery UI functions. I have noticed that if I include these inline scripts in my templates they are not evaluated. I came across this similar question https://community.jboss.org/thread/236684 but I still do not understand why the script elements are not automatically processed by the browser if there are inserted into the DOM. Can anyone provider further insight into this?

       

      I can move the inline scripts into JSNI or use the aforementioned thread's method to manually invoke the scripts but I was hoping there was an automated alternative.

        • 1. Re: Inline script elements in template files
          Aaron Anderson Newbie

          After further research and experimentation I came across the GWT SrciptInjector that suites my needs. I can create a .js file in the classpath with any arbitrary JavaScript, create a ClientBundle

           

          public interface JsResources extends ClientBundle {
              final JsResources INSTANCE = GWT.create(JsResources.class);
          
          
              @Source("com/someorg/someJS.js")
              TextResource someJS();
          
          
          }
          
          

           

          and then invoke the JavaScript in the GWT onload method inducing JavaScript execution which is almost identical to an inline script:

           

          @Templated
          public class SomeTemplate extends Composite {
          
          
              @DataField("tenants-g
              @Override
              protected void onLoad() {
                  ScriptInjector.fromString(JsResources.INSTANCE.someJS().getText()).inject();
              }
          
          
          }