Inside Webview2 when I open a new tab, a new window outside WindowsForms is open. I want to prevent this window to Open, how can I do that?
Asked
Active
Viewed 5,929 times
11
-
Does this answer your question? [How to stop webview2 from opening new browser window rather than inside the browser](https://stackoverflow.com/questions/66002331/how-to-stop-webview2-from-opening-new-browser-window-rather-than-inside-the-brow) – Poul Bak Feb 14 '21 at 18:17
-
1no, Because It can't handle with pop up windows, only with new tabs. – Tiago Gomes Feb 14 '21 at 19:21
-
There is [a good practice](https://stackoverflow.com/a/73841289/3193470) to make your app deals with new tabs like **real browsers** – 90Degree Sep 26 '22 at 14:40
3 Answers
15
You can handle CoreWebView2.NewWindowRequested
to decide about new window
- To completely suppress the popup, set
e.Handled = true;
- To show the popup content in the same window, set
e.NewWindow = (CoreWebView2)sender;
- To open in another specific instance, set
e.NewWindow
to the otherCoreWebView2
instance.
For example:
//using Microsoft.Web.WebView2.Core;
//using Microsoft.Web.WebView2.WinForms;
WebView2 webView21 = new WebView2();
private async void Form1_Load(object sender, EventArgs e)
{
webView21.Dock = DockStyle.Fill;
this.Controls.Add(webView21);
webView21.Source = new Uri("Https://stackoverflow.com");
await webView21.EnsureCoreWebView2Async();
webView21.CoreWebView2.NewWindowRequested += CoreWebView2_NewWindowRequested;
}
private void CoreWebView2_NewWindowRequested(object sender,
CoreWebView2NewWindowRequestedEventArgs e)
{
e.NewWindow = (CoreWebView2)sender;
//e.Handled = true;
}

Andrew Truckle
- 17,769
- 16
- 66
- 164

Reza Aghaei
- 120,393
- 18
- 203
- 398
2
To complement @Reza answer I was having this exact problem in VB.Net but all the answers were for C# so im going to post it in here if anyone else needs it.
First make sure to import this:
Imports Microsoft.Web.WebView2.Core
Imports Microsoft.Web.WebView2.WinForms
And then add this 2 events just replace wVBrowser with your Webview2 control name.
Private Sub wVBrowser_CoreWebView2InitializationCompleted(sender As Object, e As CoreWebView2InitializationCompletedEventArgs) Handles wVBrowser.CoreWebView2InitializationCompleted
AddHandler wVBrowser.CoreWebView2.NewWindowRequested, AddressOf CoreWebView2_NewWindowRequested
End Sub
Private Sub CoreWebView2_NewWindowRequested(ByVal sender As Object, ByVal e As Microsoft.Web.WebView2.Core.CoreWebView2NewWindowRequestedEventArgs)
e.Handled = True
End Sub

Code_Geass
- 79
- 2
- 10
2
Just be careful about where you register the WebView.CoreWebView2.NewWindowRequested
event..
I tried to use Browser.Initialized
event in which handler I would register this event handler but that event is never raised.
I had to use Browser.CoreWebView2InitializationCompleted
and now everything works just fine..

Andrew Truckle
- 17,769
- 16
- 66
- 164

Ondrej Valenta
- 126
- 3