1

I have open browsers on my application which load in data using the default web browser.

//Set the function and display the browsers we're using (per screen)
browsers[index].Width = screens[index].Bounds.Width;
browsers[index].Height = screens[index].Bounds.Height;
browsers[index].Location = new System.Drawing.Point(screens[index].Bounds.X, screens[index].Bounds.Y);

browsers[index].Navigate(new Uri(lines[index]));
browsers[index].Show();

Now my problem is that when you click a link on the page, it leaves my application and openes a new browser completely. Any way of getting out of this?

lines contains an array of URL's and browsers is an array of web pages to load up on different screens.

Richard Dickinson
  • 288
  • 1
  • 3
  • 10

1 Answers1

3

If I am not wrong this is happening because of 'TARGET = "_blank"' , I would try removing this this from the tag before the content is rendered.

private void Browser_ProgressChanged(object sender, WebBrowserProgressChangedEventArgs e)
{
    var webBrowser = (WebBrowser)sender;
    if (webBrowser.Document != null)
    {
        foreach (HtmlElement tag in webBrowser.Document.All)
        {
            if (tag.Id == null)
            {
                tag.Id = String.Empty;
                switch (tag.TagName.ToUpper())
                {
                    case "A":
                    {
                        tag.MouseUp += new HtmlElementEventHandler(link_MouseUp);
                        break;
                    }
                }
            }
        }
    }
}


private void link_MouseUp(object sender, HtmlElementEventArgs e)
{
    var 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)
            {
                AddTab(a.href);
            }
            else
            {
                CurrentBrowser.TryNavigate(a.href);
            }
            break;
        }
        case MouseButtons.Right:
        {
            CurrentBrowser.ContextMenuStrip = null;
            var contextTag = new ContextTag();
            contextTag.Element = a;
            contextHtmlLink.Tag = contextTag;
            contextHtmlLink.Show(Cursor.Position);
            break;
        }
    }
}

Source:http://stackoverflow.com/questions/5312275/open-new-web-page-in-new-tab-in-webbrowser-control

RaM
  • 1,126
  • 10
  • 25
  • You're correct, I was thinking of the issues from a C# point of view as I'm new, I didn't think it would handle it like that, evidently it has, but that's the issue. The _blank target. – Richard Dickinson May 10 '11 at 12:09
  • @Richard Dickinson, Could you mark this as answered so that other community members could focus on other questions and I would earn some reputation as well :) Thanks – RaM May 10 '11 at 12:13
  • Yes, there is a 5 minute cap on each topic so it can't be done until elapsed. – Richard Dickinson May 10 '11 at 12:16