Partilhar via


CoreWebView2.DOMContentLoaded Event

Definition

DOMContentLoaded is raised when the initial HTML document has been parsed.

public event EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2DOMContentLoadedEventArgs> DOMContentLoaded;
member this.DOMContentLoaded : EventHandler<Microsoft.Web.WebView2.Core.CoreWebView2DOMContentLoadedEventArgs> 
Public Custom Event DOMContentLoaded As EventHandler(Of CoreWebView2DOMContentLoadedEventArgs) 

Event Type

Examples

void DOMContentLoadedCmdExecuted(object target, ExecutedRoutedEventArgs e)
{
    _iWebView2.CoreWebView2.DOMContentLoaded += WebView_DOMContentLoaded;
    _iWebView2.CoreWebView2.FrameCreated += WebView_FrameCreatedDOMContentLoaded;
    _iWebView2.NavigateToString(@"<!DOCTYPE html>" +
                              "<h1>DOMContentLoaded sample page</h1>" +
                              "<h2>The content to the iframe and below will be added after DOM content is loaded </h2>" +
                              "<iframe style='height: 200px; width: 100%;'/>");
    _iWebView2.CoreWebView2.NavigationCompleted += (sender, args) =>
    {
        _iWebView2.CoreWebView2.DOMContentLoaded -= WebView_DOMContentLoaded;
        _iWebView2.CoreWebView2.FrameCreated -= WebView_FrameCreatedDOMContentLoaded;
    };
}

void WebView_DOMContentLoaded(object sender, CoreWebView2DOMContentLoadedEventArgs arg)
{
    _ = _iWebView2.ExecuteScriptAsync(
            "let content = document.createElement(\"h2\");" +
            "content.style.color = 'blue';" +
            "content.textContent = \"This text was added by the host app\";" +
            "document.body.appendChild(content);");
}

Remarks

This aligns with the the document's DOMContentLoaded event in HTML.

Applies to