Why is the Webbrowser control DocumentComplete event fired for top level frame first?

一笑奈何 提交于 2019-12-05 21:47:47
noseratio

IMO, the most reliable way to get notified when the page has been fully loaded is to attach to window.onload DOM event for the top window object (IWebBrowser2::get_Document, IHTMLDocument2::get_parentWindow), when DocumentComplete is fired for the first time for a particular navigation. Then, onload event for the top web page will be fired when all inner frames have been loaded. This answer illustrates how it can be done in C# and this answer may help to get it done in C++.

The MSDN doc seems right to me: the last DISPID_DOCUMENTCOMPLETE is the one fired for the main frame.

I can't reproduce your problem for http://www.microsoft.com/ as that link gives me the final http://www.microsoft.com/fr-fr/default.aspx which is a single frame.

I don't like the way the sample code is testing for the Main Browser (equality of 2 IDispatch pointers). What I do is this:

  1. QueryInterface the IDispatch for an IWebBrowser2
  2. Make a true COM equality test, that is "comparing the IUnknown" from the 2 IWebBrower2 (I do it with the IsEqualObject method from the CComPtr template.
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!