Default browser browsers open in iframe

I have a WebBrowser control to open links in my default browser, for example:

private void webBrowser1_Navigating(object sender, WebBrowserNavigatingEventArgs e) { if (e.Url.ToString() != "about:blank") { e.Cancel = true; System.Diagnostics.Process.Start(e.Url.ToString()); } } 

This works fine, but if I upload a document containing some IFrame elements, they will also open in the system browser (mainly embedded materials such as Google Maps, Digg icons, etc.).

How to save iframes loading in a Webbrowser control and click user links in a system browser?

+2
source share
1 answer

I came to the conclusion that the .NET WebBrowser component is practically useless in this regard. I tried to read WebBrowserNavigatingEventArgs.TargetFrameName , but it will return the name attribute of the iframe element only if it has an HTML document. Otherwise, he will spit out the empty string "" . Returning a null value for non-frame links would be more useful.

So, the only fix I found for this is using the AxWebBrowser control and, in particular, listening to the BeforeNavigate2 event. I did not test as much as I should, but it seems that the "flags" property in DWebBrowserEvents2_BeforeNavigate2Event set to 64 every time the user starts.

 private void axWebBrowser1_BeforeNavigate2(object sender, AxSHDocVw.DWebBrowserEvents2_BeforeNavigate2Event e) { // 64 means user triggered if ((int)e.flags == 64 && e.uRL.ToString() != "about:blank") { e.cancel = true; System.Diagnostics.Process.Start(e.uRL.ToString()); } } 

MSDN docs say flags is only an IE7 + parameter, so I don't know what happens on IE6 machines ...

Using Internet Explorer from .NET , there is some really valuable information about AxWebBrowser.

+2
source

Source: https://habr.com/ru/post/1494286/


All Articles