Skip to content

ExecuteScript - cWebView2Browser

Executes a script within the current top-level document of the WebView2 control

Type: Function
Return Data Type: UBigInt

Parameters

Parameter Type Description
sScript String Script to execute

Syntax

Function ExecuteScript String sScript Returns UBigInt

Call Example

Get ExecuteScript sScript to UBigIntVariable

Description

Executes a script within the current top-level document of the WebView2 control.

This method works asynchronously and will return immediately.

The result of evaluating the provided JavaScript is returned via the OnExecuteScriptFinished event.

The returned unique script id can be used to map the result to the executed script.

Sample

The example below shows how to fetch the document.title using ExecuteScript:

Object oWebView2Browser is a cWebView2Browser
    Set Size to 79 243
    Set Location to 6 3

    Procedure OnCreate
        Send NavigateToString @"
<html>
<head>
    <title>WebView Test Page</title>
</head>
<body>
    <h1>WebView Test Page</h1>
</body>
</html>"
    End_Procedure

    Procedure OnExecuteScriptFinished UBigInt iExecutionId Integer iErrorCode String sResult
        Send Info_Box sResult "Result:"
    End_Procedure  
End_Object

Object oDocTitleButton is a Button
    Set Size to 14 70
    Set Location to 89 6
    Set Label to 'Document Title'

    // fires when the button is clicked
    Procedure OnClick
        UBigInt iExecutionId

        Get ExecuteScript of oWebView2Browser @"document.title;" to iExecutionId
    End_Procedure

End_Object

Return Value

Unique identifier for the executed script