Get URL for WebBrowser.NewWindow Event
It looks like the WebBrowser
control is a really lame wrapper around SHDocVw
. Fortunately Microsoft exposes the underlying implementation through WebBrowser.ActiveXInstance
.
This code from http://www.codeproject.com/Articles/71592/How-to-easily-capture-the-NewWindow3-event-and-det will do the trick:
First, add a reference to Microsoft Internet Controls
. Then implement a NewWindow3 handler:
SHDocVw.WebBrowser axBrowser = (SHDocVw.WebBrowser)webBrowser.ActiveXInstance;
axBrowser.NewWindow3 += new DWebBrowserEvents2_NewWindow3EventHandler(Browser_NewWindow3);
Related videos on Youtube
Mave751
Updated on June 04, 2022Comments
-
Mave751 about 2 years
I'm trying to redirect new Window event to a new tab:
myWebBrowser.NewWindow += add_NewTab; //... private void add_NewTab(object sender, CancelEventArgs e) { WebBrowser thisWebBrowser = (WebBrowser)sender; e.Cancel = true; //should block the default browser to open a new window TabPage addedTabPage = new TabPage("redirected tab"); //create a new tab tabControl_webBrowsers.TabPages.Add(addedTabPage); //add the new tab to the TabControl WebBrowser addedWebBrowser = new WebBrowser() //create the new web browser inside the new tab { Parent = addedTabPage, Dock = DockStyle.Fill }; addedWebBrowser.Navigate(thisWebBrowser.StatusText.ToString()); //set the new browser destination url }
I'm not sure that using
WebBrowser.StatusText
is the best way to obtain the new window url (this doesn't work for every site I've tested).Is there a better class/method to call to get new window destination instead?
UPDATE:
I've tried the solution suggested by Charlie
- added the Microsoft Internet Control (COM) reference
- added using SHDocVw;
-
used the code:
System.Windows.Forms.WebBrowser myWebBrowser = new System.Windows.Forms.WebBrowser();
SHDocVw.WebBrowser axBrowser = (SHDocVw.WebBrowser)myWebBrowser.ActiveXInstance;
axBrowser.NewWindow3 += new DWebBrowserEvents2_NewWindow3EventHandler(Browser_NewWindow3);
Unfortunately I've received a
NullReference Exception
on third line that I wasn't able to correct.
SOLUTION:
I don't think the solution is in the related answer (or I wasn't able to find it) because it explains how to implement the NewWindow2 event (instead of NewWindow3 which handles the original destination url) and the implementation is the same suggested here which leads to the NullReference Exception error.
Anyway I've discovered this two posts:- the discussion
- the original article (the page is in Chinese but the code is readable)
The suggest is to modify the previous three lines into this one:
(myWebBrowser.ActiveXInstance as SHDocVw.WebBrowser).NewWindow3 += new SHDocVw.DWebBrowserEvents2_NewWindow3EventHandler(Browser_NewWindow3);
Everything is working now and I was able to keep using the original
System.Windows.Forms.WebBrowser
in all the rest of the code.-
Charlie over 11 yearsMy answer says NewWindow3, and so does the sample code you used to implement my answer posted above. This looks to be almost exactly the solution I provided except in your attempt to implement it you created a new instance of the web browser control and never added it to the form instead of using the control already on the form. This may be why it didn't work.
-
Mave751 over 11 yearsYes, In the "solution" paragraph I refer to the first line: This question already has an answer here. Regarding your suggest I understood I was using two different object: the "original" System.Windows.Forms.WebBrowser and "your" SHDocVw.WebBrowser without insert it into the form, but the property SHDocVw.WebBrowser.Parent wasn't writable. Thank you so much for your help, tell me if you have a cleaner solution, maybe implementing only the SHDocVw.WebBrowser.
-
GreenKiwi about 10 yearsIn case anyone else comes through here, I found that I had to set "Embed Interop Types" for the "Interop.SHDocVw" assembly to false and set the "local copy" to true. social.msdn.microsoft.com/Forums/vstudio/en-US/…
-
Kiquenet over 8 yearsDo you use StatusText for get url ?
-
Kiquenet over 8 years
-
mlvljr about 6 yearsNote: the Solution part probably worked out of coincidence, as some other part of the code got modified too -- as the way of casting the activex instance does not matter here, and the
as
cast would throw an exception just as well, if you try to add an event hander to its result
-
Mave751 over 11 yearsThank you for your help. I've followed your suggestion but received a NullReference Exception
-
mlvljr about 6 years@Mave751 I got one too, but
(SHDocVw.WebBrowser_V1)browser.ActiveXInstance
seems null only right after creation -- a quick test with a timer set to 5 s showed that it is not null later -- so a workaround could be to A) start checking for null on timer until it's not (and then cast and set up the callbacks) and B) block UI interactions or hide the browser until (A) is done. Something like that -- was wondering if I am the only one who was not happy with the "just cast it, man" advice splattered all over internets -- turns out, I'm not. -
mlvljr about 6 years..or better, just use "Form.onLoad()" -- as there, the activeX instance is not null anymore, as in stackoverflow.com/questions/6470842/… , in fact