CWebView::AsyncExecute Method  
 
BOOL AsyncExecute(
  LPCTSTR lpszJavaScript,  
  DWORD_PTR dwParam  
);

The AsyncExecute method executes a script on the current page, returning the results asynchronously.

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.
dwParam
An optional parameter which can be used to provide context to the script being executed asynchronously. This user-defined value is passed back to the event handler which is invoked when the result from the script is returned back to the application. If this information is not required, this parameter can be zero.

Return Value

If the method succeeds, the return value is non-zero. If the method fails, the return value is zero. To get extended error information, call GetLastError.

Remarks

The WebViewAsyncExecute method evaluates a JavaScript expression within the context of the currently loaded page and returns the result asynchronously. Unlike WebViewExecute, this method does not block while waiting for the script to complete. Instead, the result is delivered through the WEBVIEW_EVENT_SCRIPT_COMPLETED event. The application can use the dwParam value to correlate the completion event with the original request.

Based on how the project is configured, the AsyncExecute method will either call WebViewAsyncExecuteA, which uses 8-bit strings, or WebViewAsyncExecuteW, which uses Unicode strings. The ANSI version expects the script to be a valid UTF-8 encoded string and should not use extended ANSI characters from the system's current code page. If the lpszJavaScript parameter contains invalid Unicode characters, the method will fail and the last error code will be set to ST_ERROR_INVALID_UNICODE.

The script must be valid JavaScript and you should not assume synchronous execution. Results are returned by calling the GetWebResult method which will provide you with the response from the script. The result is provided as a JSON-formatted string, which may be either a quoted string with escaped characters or a JSON object, depending on the value returned by the script. Applications that expect structured data should parse the returned string using a JSON parser.

Because script execution is dispatched to the underlying WebView2 control, this method will fail if navigation is still in progress. Applications should always wait for the WEBVIEW_EVENT_NAVIGATION_COMPLETED event 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

// A simple event handler implementation
VOID CALLBACK WebViewEventProc(HWND hWnd, UINT uEvent, WPARAM wParam, LPARAM lParam, DWORD_PTR dwParam)
{
    CWebView *pWebView = reinterpret_cast<CWebView *>(dwParam);

    if (!pWebView)
        return;

    if (uEvent == WEBVIEW_EVENT_SCRIPT_COMPLETED)
    {
        BOOL bSuccess = (BOOL)wParam;

        if (bSuccess)
        {
            TCHAR szResult[512];
            if (pWebView->GetWebResult(szResult, _countof(szResult)))
            {
                // Get the response from the script
                // szResult will be a null-terminated string which may contain
                // a JSON object or JSON string (quoted and escaped if needed)
            }
        }
        else
        {
            // The script could not be executed
        }
    }
}

void RunExample(CWebView *pWebView)
{
    // Execute a simple script asynchronously
    if (!pWebView->AsyncExecute(_T("document.title"), reinterpret_cast<DWORD_PTR>(pWebView)))
    {
        DWORD dwError = pWebView->GetLastError();
        // Handle the error condition
    }
}  

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