Home > Enterprise >  WebView2 - How can I get the same functionality as WebBrowser.Document.OpenNew(true)
WebView2 - How can I get the same functionality as WebBrowser.Document.OpenNew(true)

Time:02-14

In System Forms Webbrowser, Below is the code to Open New HTML Document each time webrowser is updated with new HTML content

webBrowser1.Navigate("about:blank");
webBrowser1.Document.OpenNew(true).Write(html);

In Microsoft.Web.WebView2.WinForms.WebView2 I can use navigate to string like below to update HTML string.

webBrowser.NavigateToString(html);

But this doesn't open a new document. It updates on same. Is there a way in WebView2 to set HTML content on the new document each time content is changed?

CodePudding user response:

There's a javascript document.open(type, replace) that can help you to achieve the same functionality as the old WebBrowser control's Document.OpenNew(replaceInHistory).

To use it with WebView2, you need to call ExecuteScriptAsync, for example:

await webView21.EnsureCoreWebView2Async();
await webView21.ExecuteScriptAsync(
    "document.open('text/html', true);"  
    "document.write('<html><body>XXXXXXXXXX</body></html>');");

It will open a new document and replaces the history.

  • Related