Partilhar via


CoreWebView2.ExecuteScriptWithResultAsync(String) Method

Definition

Runs JavaScript code from the javaScript parameter in the current top-level document rendered in the WebView, The result of the execution is returned asynchronously in the CoreWebView2ExecuteScriptResult object which has methods and properties to obtain the successful result of script execution as well as any unhandled JavaScript exceptions.

public System.Threading.Tasks.Task<Microsoft.Web.WebView2.Core.CoreWebView2ExecuteScriptResult> ExecuteScriptWithResultAsync (string javaScript);
member this.ExecuteScriptWithResultAsync : string -> System.Threading.Tasks.Task<Microsoft.Web.WebView2.Core.CoreWebView2ExecuteScriptResult>
Public Function ExecuteScriptWithResultAsync (javaScript As String) As Task(Of CoreWebView2ExecuteScriptResult)

Parameters

javaScript
String

Returns

Examples

var dialog = new TextInputDialog(
    title: "Inject Script With Result",
    description: "Enter some JavaScript to be executed in the context of this page, and get the error info when execution failed",
    defaultInput: ""
);
if (dialog.ShowDialog() == true)
{
    var result = await _iWebView2.CoreWebView2.ExecuteScriptWithResultAsync(dialog.Input.Text);
    if (result.Succeeded)
    {
        MessageBox.Show(this, result.ResultAsJson, "ExecuteScript Json Result");
        int is_success = 0;
        string str = "";
        result.TryGetResultAsString(out str, out is_success);
        if (is_success == 1)
        {
            MessageBox.Show(this, str, "ExecuteScript String Result");
        }
        else
        {
            MessageBox.Show(this, "Get string failed", "ExecuteScript String Result");
        }
    }
    else
    {
        var exception = result.Exception;
        MessageBox.Show(this, exception.Name, "ExecuteScript Exception Name");
        MessageBox.Show(this, exception.Message, "ExecuteScript Exception Message");
        MessageBox.Show(this, exception.ToJson, "ExecuteScript Exception Detail");
        var location_info = "LineNumber:" + exception.LineNumber + ", ColumnNumber:" + exception.ColumnNumber;
        MessageBox.Show(this, location_info, "ExecuteScript Exception Location");
    }
}

Applies to