0

When trying to inject jQuery through ScriptInjector, this is the error thrown when $wnd.$ is called through JSNI:

Caused by: com.google.gwt.core.client.JavaScriptException: (TypeError): Object [object global] has no method '$' at com.google.gwt.dev.shell.BrowserChannelServer.invokeJavascript(BrowserChannelServer.java:248) at com.google.gwt.dev.shell.ModuleSpaceOOPHM.doInvoke(ModuleSpaceOOPHM.java:136) at com.google.gwt.dev.shell.ModuleSpace.invokeNative(ModuleSpace.java:561) at com.google.gwt.dev.shell.ModuleSpace.invokeNativeVoid(ModuleSpace.java:289) at com.google.gwt.dev.shell.JavaScriptHost.invokeNativeVoid(JavaScriptHost.java:107)

Here is the code to inject jQuery:

ScriptInjector.fromUrl("http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js")
            .setWindow(ScriptInjector.TOP_WINDOW).setCallback(new Callback<Void, Exception>() {
                @Override
                public void onSuccess(Void arg0) {
                    GWT.log("Success to load jQuery library");
                    ScriptInjector.fromUrl("http://ajax.googleapis.com/ajax/libs/jqueryui/1.8.23/jquery-ui.min.js").setWindow(ScriptInjector.TOP_WINDOW).inject(); 
                }

                @Override
                public void onFailure(Exception arg0) {
                    GWT.log("Failed to load jQuery library");
                }
            }).inject();

What could be the problem?

3 Answers 3

4

Loading of external javascript files with ScriptInjector.fromUrl() is performed asynchronously, so probably you are trying to call the $wnd.$ before jQuery has been loaded. Put a delay in your call or use the onSuccess to continue the workflow of your app.

BTW, if you are interested on using jquery-ui in your app, you could take a look to the Gwtquery-ui plugin, which does automatically the loading of javascript dependencies, it is not a pure gQuery plugin and depends on jQuery and jQuery-ui, but it is has a nice integration with gwt and gQuery.

[EDITED]

A NEW feature I've recently added to gQuery (1.4.0-SNAPSHOT) is a JsniBundle for including external javascript as JSNI blocks:

public interface JQuery extends JsniBundle {
   @LibrarySource("http://ajax.googleapis.com/ajax/libs/jquery/1.8.1/jquery.min.js")
   // You can use javascript files placed in your source tree as well
   // @LibrarySource("jquery.js")
   public void load();
}

// Generate the Bundle implementation
JQuery jQuery = GWT.create(JQuery.class);
// Load the third-party library
jQuery.load();

The goals of using JsniBundle are:

   - Use pure javascript files so as we can use IDEs for editing, formating etc,
     instead of dealing with code in comment blocks.
   - Facilitate writing and testing javascript in the browser before compiling it.
   - Include third-party javascript libraries without modification of the original source.
   - Not need of adding javascript tags in the html page or module file to include
     third-party javascript.
   - GWT compiler will get rid of any jsni fragment if the application does not use it.
   - Included javascript will take advantage of GWT jsni validators, obfuscators
     and optimizers.
Sign up to request clarification or add additional context in comments.

4 Comments

Hi Manolo I use GwtQuery ;-) But for some specific jquery based JS libraries I need to include this.
If you use gQuery you couldbe interested in JsniBundle just for doing this. Take a look to my edited comment.
how do you inject using JsniBundle a js located in /clien/resources/js/some.js ?
It looks in the folder of the class during compile time, so you have to use ../ until you get the folder with the js.
1

The correct syntax is to use $wnd.jQuery instead of $wnd.$ I think it has something to do with $ being reserved in the gwt iframe.

4 Comments

$wnd.$ works fine when jQuery is inserted in HTML not through ScriptInjector
@xybrek, I agree, but the question specifically mentions scriptinjector
So if I use ScriptInjector, then I should use $wnd.jQuery instead? +1 on that.
ScriptInjector injects jQuery in the $wnd context because of the setWindow(WINDOW), and apart from be available as $wnd.jQuery will being available as $wnd.$ unless you call $wnd.jQuery.noConflict() after inserting the library.
0

Try to load your jquery snippet code in the success callback, that is, when the jquery lib ends to load.

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.