Calling JavaScript from the Server
callJsFunctionMethodexecuteJsMethod- Return Values
- Passing JavaScript Functions [since:com.vaadin:vaadin@V25.2]
The Element API contains methods for executing JavaScript in the browser from the server side.
callJsFunction Method
The Element.callJsFunction() method allows you to run a client-side component function from the server side. The method accepts two parameters: the name of the function to call; and the arguments to pass to the function.
The arguments passed to the function must be a type supported by the communication mechanism. The supported types are String, Boolean, Integer, Double, JsonNode, Element, Component, and JsFunction (see Passing JavaScript Functions).
clearSelection() JavaScript function on the root element from the server sideSource code
Java
public void clearSelection() {
getElement().callJsFunction("clearSelection");
}expand(otherComponentElement) JavaScript function on the root element from the server sideSource code
Java
public void setExpanded(Component otherComponent) {
getElement().callJsFunction("expand",
otherComponent.getElement());
}Source code
Java
public void configure(String label, int count) {
ObjectNode config = JacksonUtils.createObjectNode();
config.put("label", label);
config.put("count", count);
config.put("enabled", true);
getElement().callJsFunction("configure", config);
}executeJs Method
You can also use the generic Element.executeJs() method to run JavaScript asynchronously from the server side. This method can be used in addition to the Element.callJsFunction() method when calling any JavaScript.
The executeJs() method accepts two parameters: the JavaScript expression to invoke; and the parameters to pass to the expression. The given parameters are available as variables named $0, $1, and so on.
The arguments passed to the expression must be a type supported by the communication mechanism. The supported types are String, Integer, Double, Boolean, JsonNode, Element, Component, and JsFunction (see Passing JavaScript Functions).
MyModule.complete(true) on the client sideSource code
Java
public void complete() {
getElement().executeJs("MyModule.complete($0)", true);
}Source code
Java
public void setItems(List<String> items) {
getElement().executeJs("this.items = $0", items);
}|
Warning
|
Avoid Script Injection Vulnerabilities
Always pass arguments using the $0, $1, … notation to avoid script injection vulnerabilities. Never concatenate or interpolate strings to build JavaScript code to be executed.
|
If you need to run JavaScript without having access to an element, use the UI.getCurrent().getPage().executeJs() method.
Return Values
The return value from the JavaScript function called using callJsFunction(), or the value from a return statement in an executeJs() expression can be accessed by adding a listener to the PendingJavaScriptResult instance returned from either method.
Source code
Java
public void checkConstructableStylesheets() {
getElement().executeJs(
"return 'adoptedStyleSheets' in document")
.then(Boolean.class, supported -> {
if (supported) {
System.out.println(
"Feature is supported");
} else {
System.out.println(
"Feature isn't supported");
}
});
}If the return value is a JavaScript Promise, the client only sends the return value to the server when the Promise is resolved.
Deserializing Generic Types with TypeReference
The then() method accepts a Class parameter for simple types, but this doesn’t work for generic types like List<Person> due to Java type erasure. For these cases, use the Jackson TypeReference overloads.
List<Person>Source code
Java
getElement().executeJs("return this.getItems()")
.then(new TypeReference<List<Person>>() {},
items -> {
// items is List<Person>
items.forEach(person ->
System.out.println(person.getName()));
});An error handler can be provided as a second callback. The handler receives the error message from the failed JavaScript execution as a String:
Source code
Java
getElement().executeJs("return this.getItems()")
.then(new TypeReference<List<Person>>() {},
items -> processItems(items),
errorMessage -> handleError(errorMessage));You can also use toCompletableFuture(TypeReference) to get the result as a CompletableFuture:
Source code
Java
CompletableFuture<Map<String, Person>> future = getElement()
.executeJs("return this.getPersonMap()")
.toCompletableFuture(
new TypeReference<Map<String, Person>>() {});Passing JavaScript Functions [version-badge-upcoming]
A JsFunction lets you build a reusable JavaScript function on the server and pass it as a parameter to executeJs() or callJsFunction(). The function arrives on the client as a real callable function with its captured values pre-bound, so you don’t need to concatenate JavaScript fragments to embed server-side values.
The first argument to JsFunction.of() is a JavaScript function body. The remaining arguments are captured values, referenced inside the body as $0, $1, … using the same naming convention as executeJs() parameters.
Source code
Java
JsFunction greet = JsFunction.of(
"return $0 + ' ' + $1;", "Hello", "World");
getElement().executeJs(
"this.textContent = $0();", greet);Captures may be any value supported as an executeJs() parameter. An attached Element arrives as the corresponding DOM node; a detached Element arrives as null.
Source code
Java
Div target = new Div();
add(target);
JsFunction mutate = JsFunction.of(
"$0.textContent = 'updated';",
target.getElement());
getElement().executeJs("$0();", mutate);Declaring Runtime Arguments
Use withArguments() to declare named parameters that the function accepts at call time. The body references them by name, and the JavaScript that invokes the function passes them positionally:
Source code
Java
JsFunction format = JsFunction
.of("return prefix + ':' + suffix;")
.withArguments("prefix", "suffix");
getElement().executeJs(
"this.textContent = $0('alpha', 'beta');", format);Controlling this
In JavaScript, the value of this inside a function depends on how the function is invoked, not where it is defined. The body of a JsFunction follows this convention – this is not bound to the host element automatically.
When the function is invoked as $0(…) from inside an executeJs() expression, the body’s this is the global object (or undefined in strict mode), not the element on which executeJs() was called. To set this explicitly, invoke the function with Function.prototype.call(): its first argument becomes this inside the body.
thisSource code
Java
JsFunction setOwnText = JsFunction
.of("this.textContent = msg;")
.withArguments("msg");
getElement().executeJs(
"$0.call(this, 'host element text');", setOwnText);If the body always needs to reference the same element regardless of how the function is called, pass it as a capture instead of relying on this.
AB7EDF45-DB22-4560-AF27-FF1DC6944482