CWebView::Execute Method  
 
INT Execute(
  LPCTSTR lpszJavaScript,  
  LPTSTR lpszResult,  
  INT nMaxLength  
);
INT Execute(
  LPCTSTR lpszJavaScript,  
  CString& strResult  
);

The Execute method executes JavaScript and returns the result synchronously.

Parameters

lpszJavaScript
A null-terminated string which specifies the JavaScript that should be executed in the context of the current page being viewed. This parameter is required and cannot be NULL.
lpszResult
A string buffer which will contain the result returned by the script, either as a JSON object or a JSON string value. If this parameter is NULL, the method will not copy the result but will return the total number of characters in the response, not including the terminating null. An alternate version of this method accepts a reference to a CString object which will contain the result.
nMaxLength
The maximum number of characters which can be copied into the string buffer, including the terminating null. If the lpszResult parameter is NULL, this parameter should be zero.

Return Value

If the method succeeds, return value is the number of characters in the result string, not including the terminating null character. If the method fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

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.

Example

// Execute a simple script that returns a string
INT nLength = pWebView->Execute(_T("document.title"), NULL, 0);

if (nLength > 0)
{
    // Allocate a buffer large enough to hold the result
    LPTSTR lpszResult = (LPTSTR)malloc((nLength + 1) * sizeof(TCHAR));
    if (lpszResult)
    {
        // Retrieve the result of the script execution
        if (pWebView->GetWebResult(lpszResult, nLength + 1))
        {
            _tprintf(_T("Page title is: %s\n"), lpszResult);
        }
        free(lpszResult);
    }
}
else
{
    _tprintf(_T("Script execution failed\n"));
}

Requirements

Minimum Desktop Platform: Windows 7 Service Pack 1
Minimum Server Platform: Windows Server 2008 R2 Service Pack 1
Header File: cstools12.h
Import Library: cshtmv12.lib
Unicode: Implemented as Unicode and ANSI versions

See Also