View.evaluateJavascript()

Overview

Asynchronously executes some JavaScript in the WebView engine.

When connecting to a web host, there are two JavaScript engines that run.

The Velocity JavaScript engine maintains the current state of the session using scopes as outlined.

The WebView JavaScript engine has access to the DOM, and executes all JavaScript coming from the web server.

Most Velocity APIs run on the Velocity JavaScript engine. Use the View.evaluateJavascript function to execute JavaScript using the WebView engine, instead. The result can then be used by the Velocity JavaScript engine.

Added in version 1.2.109

Format

var result = View.evaluateJavascript(javascript, resultDelegate);

Parameter Description Type Required Notes
javascript A string containing JavaScript to evaluate using the WebView JavaScript engine. String Required The JavaScript evaluation is asynchronous and will not block execution of the current script.
resultDelegate A function that should be called with the result from the executed JavaScript. Function with a single String parameter Optional This allows the Velocity JavaScript engine to use the result of the executed JavaScript.
result Returns true if the view is open for this session. Returns false if the view is not available. Boolean Return Value

Example 1

Copy
/* Show an alert on the web view.
 */
 
View.evaluateJavascript("alert('hello');");

Example 2

Copy
/* Get a result back from the web view.
 */
 
View.evaluateJavascript("document.getElementById('field1').value", function(result) {
    /* this happens sometime in the future */
    Logger.info("the value of the field is " + result);
});