How to force popup window to navigate in the same window in a webbrowser control

When clicking on an link that targets a new window,  the default behavior of a webbrowser control host application is to open a new window in Internet Explorer. User ben_lai wants to know how to handle the click and navigate to the same window.

There are three events that notifies a new window, NewWindow (The current documentation of this event is wong, check http://support.microsoft.com/kb/185538/EN-US/ for the parameters of the event), NewWindow2 and NewWindow3, each one has a cancel parameter to stop the new window being created, though for NewWindow2, it requires cooperation of BeforeNavigate2 to get the url of the new window. After getting the url, the navigation can be carried out in the original window by calling Navigate2.

The obsolete NewWindow Event has the url right in the parameter so you can cancel the navigation and call the navigation methods of the webbrowser control with the new url. The problem is, the event is there for compatibility purposes and you don’t know how long the event will still be raised. And it is not without problem, one fixed bug causes a 60 seconds delay on new window creation, another causes the navigation to fail. The fix for the latter? use NewWindow2.

The NewWindow2 event, introduced in IE4, does not have the url parameter. So you cannot simply cancel the event. To get the url, you need do the normal processing, create a hidden new webbrowser host window and assign the webbrowser to the event’s ppdisp parameter, then hook BeforeNavigate2 in the new window to get the url (may require client to patch their Windows if they are on Windows Vista). Once you get the url, you can destroy the new window and navigate in the old window.

The NewWindow3 event, introduced with Windows Server 2003 SP1 and Windows XP SP2, is designed for the webbrowser host to get notified of the decision of the popup window manager based on user preference (for example, if the new window should be opened in a new window or a new tab in the current window), though the popup window manager’s behavior can be customized too. For the purpose of cancelling navigation and navigate to the old window, it does not matter how the new window should be opened, so you can just take the url and execute the same logic as in the NewWindow event. Since everyone should be using IE7 or higher now, the event should be supported by the browser. If the client can’t upgrade IE, at least ask them to fully patch their Windows.

The original poster’s question is in the VB forum so all the native events are available. For .net programmers, you can hook up the event via IConnectionPointContainer directly if you are inserting the webbrowser as an ActievX on a Windows Form, or extend the webbrowser site if you are using the System.Windows.Forms.Webbrowser class. Update: WPF users who want to use System.Windows.Controls.WebBrowser are out of luck, they need to host the ActiveX versions of the webbrowser control to get a hand on the native events.

About Sheng Jiang 蒋晟

Microsoft MVP in Visual C , 2004- Forum moderator of the Visual C and .Net forums on CSDN Forum moderator of Chinese forums on Microsoft's MSDN forums
This entry was posted in enmsdn, Microsoft, Visual C++, Visual Studio, Webbrowser control and tagged , , , , , , , . Bookmark the permalink.

3 Responses to How to force popup window to navigate in the same window in a webbrowser control

  1. Bubba Bubbs says:

    Hi,

    I am currently finishing up a C++ BHO project for my company, and I discovered a pretty critical bug a lot later in the process than I had hoped. I have a BHO that currently uses SINK to capture BeforeNavigate2, NavigateComplete2, NavigateError, and several other events, but I am running into an issue capturing 302 redirect URLs.

    So for example, lets say we have 4 sites:

    a starting site A that has a link to B a site B that 302 redirects to site C a site C that 302 redirects to site D, a final site D

    The user starts on site A: clicks site B: site B redirects to site C, site C redirects to site D: user winds up at site D.

    In this scenario, my BeforeNavigate2 will capture site B’s URL, and my NavigateComplete will capture site D’s URL. But site C is completely invisible to my BHO. You could add any number of 302s in-between B and D in this scenario and I’m not able to capture any of them.

    Note: I solved this in Internet Explorer 7 by adding a DOCHOSTUIFLAG_ENABLE_REDIRECT_NOTIFICATION to the GetHostInfo flag, which made every redirect trigger a BeforeNavigate2. But it appears IE8 does not respect this flag😦

  2. Soomya Singh says:

    Dear Friend,

    Any working code of this, i am able to cancel the new window opening in default browser by using e.cancel=true but i can not get the url which it was opening so i can open it in another webbrowser control.

    Do you have any example to do so?

    Thanks

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