I need to use web browser in my application as it keeps repetitive tasks from employees, but there is a problem with javascript that opens a new window in IE after clicking on anchor. How do I tell web browser control to "open new window" where I want it to be opened? For example in the other web browser control?
Check out: proof-of-concept of .NET System.Windows.Forms.WebBrowser module using source code
My experience about that controls has given me a vision that this issue can tried to be solved in next steps:
always cancel NewWindow event
catch all links clicking
but not all link can be cached this way, so I decided to parse all tags <a>
manually on Document Loading Completion
in general, this control is very poor and has been made so by Microsoft deliberately. though there is powerful toolset around Webrowser.Document.HtmlDocument and namespace MSHTML
an example of it's using is HtmlElement.DomElement
foreach(HtmlElement tag in webBrowser.Document.All)
{
switch (tag.TagName.ToUpper)
{
case "A":
{
tag.MouseUp += new HtmlElementEventHandler(link_MouseUp);
break;
}
}
}
void link_MouseUp(object sender, HtmlElementEventArgs e)
{
HtmlElement link = (HtmlElement)sender;
mshtml.HTMLAnchorElementClass a = (mshtml.HTMLAnchorElementClass)link.DomElement;
switch (e.MouseButtonsPressed)
{
case MouseButtons.Left:
{
if ((a.target != null && a.target.ToLower() == "_blank") ||
e.ShiftKeyPressed ||
e.MouseButtonsPressed == MouseButtons.Middle)
{
// add new tab
}
else
{
// open in current tab
}
break;
}
case MouseButtons.Right:
{
// show context menu
break;
}
}
}
See more at the first link, that's the source code of main window, there are a lot of different manipulations there!
I found a simple solution that works.
private void WebBrowser1_NewWindow(object sender, System.ComponentModel.CancelEventArgs e) {
e.Cancel = true;
WebBrowser1.Navigate(WebBrowser1.StatusText);
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With