Executing JavaScript

You can use server-side Java to execute simple JavaScript snippets in the browser. You can also pass parameters to the executed script as variables named $0, $1 and so on. The framework automatically serializes and escapes the parameter values.

public static void logElementSize(String name, Element element) {
    Page page = UI.getCurrent().getPage();

    page.executeJavaScript(
            "console.log($0 + ' size:', $1.offsetWidth, $1.offsetHeight)",
            name, element);
}

Supported parameter types are String, Boolean, Integer, Double, JsonValue and Element. The script is executed after the DOM tree has been updated based on server-side changes. The parameter value will be null for an element parameter that is not attached after the update (according to the server-side DOM).

Note
The script is executed asynchronously, so you can’t directly pass values back to the server.