Re: Loading third-party Javascript with a script loader

341 views
Skip to first unread message

Joseph Lust

unread,
Jun 27, 2012, 2:51:02 PM6/27/12
to google-we...@googlegroups.com
At the moment we have quite a few script files and the only way we were able to get GWT and our product to work together is by adding all imports manually into the HTML file (which forces them to be loaded before GWT). 

Could you please explain why this is the only way they will work? What happens when you use "<script src=.../>" tag in your module.gwt.xml?

At first glance, I assume that your scripts must require that certain page elements already be available before they run. In that case, you could try loading them in the module via the gwt.xml file, then use a very simple JSNI method from your onModuleLoad() to initialize the scripts. This way your scripts could be placed into the page at load, but only triggered when GWT was ready for them.

Hope that helps.

Sincerely,
Joseph

Bakul

unread,
Jun 28, 2012, 6:02:54 PM6/28/12
to google-we...@googlegroups.com
Hi,

You can add javascript asynchronously whenever you want and invoke any function from that js using JSNI.

Please refer following 

You will probably need something like following to add js in the page and then you can invoke the method from your js using JSNI

public void addScript(String url) {
    Element e = DOM.createElement("script");
    DOM.setAttribute(e, "language", "JavaScript");
    DOM.setAttribute(e, "src", url);
    DOM.appendChild(RootPanel.get().getElement(), e);
} 

JoseM

unread,
Jun 28, 2012, 7:48:30 PM6/28/12
to google-we...@googlegroups.com
You can also try using ScriptInjector, and make sure you set the Window to the one you want ($wnd).  They provide a callback to let you know when it is loaded:


Reply all
Reply to author
Forward
0 new messages