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.

Java

  1. public static void logElementSize(String name, Element element) {
  2. Page page = UI.getCurrent().getPage();
  3. page.executeJavaScript(
  4. "console.log($0 + ' size:', $1.offsetWidth, $1.offsetHeight)",
  5. name, element);
  6. }

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.