2

How do I add a script such as the jQuery library so it is available for use in GWT? I know how to do JSNI, I just don't know how to make the jQuery library available to me so I can call it within JSNI.

2 Answers 2

3

GWT can call any JS function or reference any JS variable on the page using JSNI, so as long as the JQuery script is also loaded on the page, you can call any JQuery code you want.

For example:

public native void callJQuery() /*-{
  $("p.neat").addClass("ohmy").show("slow");
}-*/;
Sign up to request clarification or add additional context in comments.

2 Comments

please let me know if you ever figure out how to check and uncheck a radio button.
It's trivial to check/uncheck a radio button with regular GWT, why would you need to involve JQuery?
2

They don't play well together, yet. The only way I know so far (correct me if I'm mistaken) is GWTQuery. Give it a try ;)

EDIT

I re-explained my thoughts, hope it'll be more clear now

It is possible to call any desired custom JavaScript code, see Jason's answer.

I recommend using GWTQuery, since it's successfull port of JQuery to Java-based GWT ecosystem and doesn't force you to use raw native JavaScript from Java code.

4 Comments

What do you mean by "they don't play well together"?
@Jason Hall -> You can do it like you posted, and by that I meant 'not play well'. I believe GWT should be focused on Java whenever possible. In other words, I prefer avoiding 'native JS hacks'. GWTQuery ports JQuery into GWT ecosystem really nicely I think. But this might just matter of my opinion, though.
That's fair, I agree that GWT apps should be Java-focused, that's the whole point of it after all, but your comment made it seem like it wasn't possible, or easy, to call JQuery from GWT, which isn't true. GWTQuery is a great alternative to calling JQuery in GWT, since it also avoids downloading both JQuery and GWT code.
I admint it might have looked confusing, so I edited my answer :)

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.