0

I have a web browser control in my application which contains the following link:

<html> <body> <a href="http://www.google.com" target="abc">test</a> </body> </html> 

Each time I click on this link, it gets opened in a new window of IE instead of a new Tab. I tried loading this html directly in IE - then it correctly gets opened in new tabs. I have also configured the IE setting to open links in new tabs instead of new windows.

Can anyone help me out to load the links from the web browser control in a new tab? Thank you!

1

1 Answer 1

0

It would have been helpful if you had specified whether you were using Winforms or WPF for your web browser control or even what language you were using (C#, VB,F#, etc) but assuming you are using winforms and C# this solution would work.

You simply cancel the new window event and handle the navigation and tab stuff yourself.

Here is a fully working example.

using System.ComponentModel; using System.Windows.Forms; namespace stackoverflow2 { public partial class Form1 : Form { public Form1() { InitializeComponent(); this.webBrowser1.NewWindow += WebBrowser1_NewWindow; this.webBrowser1.Navigated += Wb_Navigated; this.webBrowser1.DocumentText= "<html>"+ "<head><title>Title</title></head>"+ "<body>"+ "<a href = 'http://www.google.com' target = 'abc' > test </a>"+ "</body>"+ "</html>"; } private void WebBrowser1_NewWindow(object sender, CancelEventArgs e) { e.Cancel = true; //stop normal new window activity //get the url you were trying to navigate to var url= webBrowser1.Document.ActiveElement.GetAttribute("href"); //set up the tabs TabPage tp = new TabPage(); var wb = new WebBrowser(); wb.Navigated += Wb_Navigated; wb.Size = this.webBrowser1.Size; tp.Controls.Add(wb); wb.Navigate(url); this.tabControl1.Controls.Add(tp); tabControl1.SelectedTab = tp; } private void Wb_Navigated(object sender, WebBrowserNavigatedEventArgs e) { tabControl1.SelectedTab.Text = (sender as WebBrowser).DocumentTitle; } } } 
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.