Posted by: dotnetninja | February 26, 2008

Prevent opening new window from WebBrowser control

While ago I was creating customized browser for use in one terminal application which was created with WPF. One of the “pages” on the terminal should be browser, where link that opens in the new window should be redirected to the current window (browser control).

WebBrowser controls has DocumentCompleted event which occurs when control is fully loaded. So my idea was after control was loaded to parse all links in the current page after document has loaded and to attach onclick event to each one of the links and cancel opening new window.

private string url = “”;

public SimpleBrowser()
{
this.InitializeComponent();

WinFormBrowser.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(WinFormBrowser_DocumentCompleted);

WinFormBrowser.NewWindow += new System.ComponentModel.CancelEventHandler(WinFormBrowser_NewWindow);
}
 

void WinFormBrowser_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
{
HtmlElementCollection links = WinFormBrowser.Document.Links;
foreach (HtmlElement var in links)

{
var.AttachEventHandler(“onclick”, LinkClicked);
}
}

After attaching click event to links we can use LinkClicked function to save latest clicked link in the browser.

private void LinkClicked(object sender, EventArgs e)
{

HtmlElement link = WinFormBrowser.Document.ActiveElement;
url = link.GetAttribute("href");
}

Now on the NewWindow event we intercept and cancel opening new window and instead navigate in browser with latest clicked link.

void WinFormBrowser_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
{
e.Cancel = true;
WinFormBrowser.Navigate(url);

}


Responses

  1. I think you can use this way.

    void WinFormBrowser_NewWindow(object sender, System.ComponentModel.CancelEventArgs e)
    {
    WebBrowser webBrowser = (WebBrowser)sender;
    HtmlElement link = webBrowser.Document.ActiveElement;
    Uri urlNavigated = new Uri(link.GetAttribute(“href”));
    WinFormBrowser.Navigate(url);
    e.Cancel = true;
    }

    • Thanks a lot. It works perfectly…

  2. Any ideas how to handle JS confirmation messages? I have made own browser, for some web applications functions automation, but those applications have JS confirmation dialogs, where I have to click Ok or Cancel, so how to automate those click from C# code?

  3. Your post solves one of my problem.

    I am developing an application for secure browsing. Only sites that are permitted can be opened. I want to uninstall IE and other browsers from the client’s machine. I came to know that the WebBrowser control is dependent on IE and a file need to be supplied while deploying my C# application.

    Can you help further?

  4. Best solution I’ve found to this problem is here – who would have thought the easiest way would be to cast the new .net WebBrowser control to a copy of the older one to access it’s long lost NewWindow3?

    http://social.msdn.microsoft.com/Forums/en-US/winforms/thread/f497f8a5-dac8-48cb-9fce-7936c9389f09

  5. Been trying to implement this coding into my own project but the coding to catch the href of the link is not working for me. Do you know of an alternative way to do this?

    • there is a comment from John Sims with link, maybe that?

  6. I have found the following situation:

    Sometimes the click is performed in a subelement of an tag. In that case, you will not get the url you need, since that subtag does not have a href attribute.

    In order to fix this, you should check that the clicked tag is actually an tag. Otherwise, move up to parents until you found the first tag and from that one you get the href you need.

  7. I have found the following situation:

    Sometimes the click is performed in a subelement of an anchor tag. In that case, you will not get the url you need, since that subtag does not have a href attribute.

    In order to fix this, you should check that the clicked tag is actually an anchor tag. Otherwise, move up to parents until you found the first tag and from that one you get the href you need.

  8. Thanks for finally talking about >Prevent opening new window from WebBrowser control Arman Kurtagic’s Weblog <Loved it!

  9. Thank you for the excellent knowledge, this was a great guide.
    I will examine it even more in the future and mail it along to a couple of of my friends that will be interested

  10. Best solution i found in your article………awesome artice
    very helpful


Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

%d bloggers like this: