WebBrowser control: Detect navigation failure

泪湿孤枕 提交于 2019-12-01 20:34:49

Internet Explorer and Windows have an extensible list of available protocols implemented in UrlMon.dll, I believe. See here for a bit about IE architecture.

The reason you cannot detect the bad protocol in BeforeNavigate is that the protocol is unknown, so no real navigation is happening. The browser decides to show an error page instead. Error page navigation does not evidently raise all the normal events.

However, there is a way to detect when navigation has gone in the weeds. If you hook up to the web browser's DocumentCompleted event, you can scan for particular URLs associated with errors, or more generally, anything with a URL that starts with res://ieframe.dll.

Examples:

  • res://ieframe.dll/unknownprotocol.htm#spp:CloseWindow
  • res://ieframe.dll/dnserrordiagoff_webOC.htm#http://192...

A cleaner way is to hook into the NavigateError of the DWebBrowserEvents2 interface.

We had a problem when hosting a web browser control (Google Map) in that we would be notified that navigation was complete (NavigateComplete), however the web page itself hadn't finished rendering. To fix this issue, we added a notifyInitialised javascript function that simply navigated to 'app://onInitialised' - a similar mechanism that you're using.

Perhaps you could so something like this (if you have control over the pages to which the user is navigating to). You could add this notification mechanism and check for it in your code. If it's not received after a prescribed timeout you could assume something's gone wrong and display a relevant message.

If you're interested, we also used a mechanism for directly calling javascript functions from our C++ code described here and here.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!