The Execute method evaluates a JavaScript
expression within the context of the currently loaded page and returns
the result synchronously. Unlike AsyncExecute, this method
blocks the current thread while waiting for the script to complete. If
the web page is non-responsive, this method will timeout after
approximately two seconds.
Based on how the project is configured, the Execute method
will call either WebViewExecuteA, which uses 8-bit strings,
or WebViewExecuteW, which uses Unicode strings. The ANSI
version
returns the result as a UTF-8 encoded string, while the Unicode
version
returns a UTF-16 string. For the ANSI version, the returned length
represents the number of bytes in the UTF-8 result, excluding the
terminating null character. Because UTF-8 uses a variable number of
bytes to represent Unicode characters, the returned length may be
greater than the number of characters in the string. If the result is
truncated, the method ensures that a multibyte UTF-8 sequence is not
split.
This method returns the total length of the script result,
excluding the terminating null character, regardless of the size of
the result buffer. If the buffer is too small, the result is truncated
and null-terminated. To dynamically allocate a buffer for the complete
result, call Execute with the lpszResult
parameter as NULL and the nMaxLength parameter as zero. Then
allocate a buffer using the returned length plus one for the null
terminator. Call GetWebResult to retrieve the complete
result without executing the JavaScript again. Calling Execute
a second time will execute the script again, which may produce a
different result or have unintended side effects.
The result returned by this method is the JSON representation
produced by the WebView2 runtime, rather than a plain-text
representation of the JavaScript value. The result may be a quoted
string, number, boolean, array, object, or the JSON value null.
String results are enclosed in quotation marks and may contain escaped
characters. Applications that need the original string value or
structured data should parse the result using a JSON parser.
If the JavaScript expression evaluates to an undefined value or
throws an unhandled exception, the WebView2 runtime may return the
JSON value null. This value does not necessarily indicate that
the script executed successfully, and it cannot be used to distinguish
an exception from a script that explicitly returns null. The
method's return value indicates the length of the result string, not
whether the JavaScript expression completed without an exception.
Because script execution is dispatched to the underlying WebView2
control, this method will fail if navigation is still in progress.
Applications should wait for the WEBVIEW_EVENT_NAVIGATION_COMPLETED
event notification before invoking script execution.
JavaScript must be enabled for scripts to execute. The
WEBVIEW_OPTION_ENABLE_SCRIPTING option must be specified
when the browser is created or attached to a window. If scripting is
disabled, this method will fail indicating that the operation is
not supported.
The AddStartupScript method can be used to insert
JavaScript which is executed whenever a new document is loaded.