JSNI

Reference

www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html

Introduction

GWT translates Java into JavaScript

At sparse times direct JS codes are needed. This can be done with JSNI methods provided by GWT.

// specially formatted comment block

public static native void alert(String msg) /*-{

$wnd.alert(msg);

}-*/;

They can be static (like above) or instance methods.

Of course, working with JS loses type safe protection.

Bridging between Java and Javascript codes

At compile time GWT compiler performs some syntax checks on the JS code between the comment tokens

Method arguments and return values

GWT generate interface code for converting method arguments and return values properly.

* Java Varargs (variable length arguments): Supported from GWT 1.5. Calling a varargs JS method from Java will result in the callee receiving the arguments in an array.

For other details, just read the GWT reference: www.gwtproject.org/doc/latest/DevGuideCodingBasicsJSNI.html

Accessing Browser Window and Document Objects in JS code

Because the compiled script runs in a nested frame, $wnd and $doc are initialized to refer to the host page's window and document

  • $wnd for window

  • $doc for document