Document Complete event for multiple frames

Forgive a newbie-ish question. :)
I find that with complicated multi-frame pages (e.g. www.cnn.com) I get a document complete event for every single frame. Most of these are ad frames of course. Is there a way to obtain the "real" URL of the site, i.e. www.cnn.com in this case, without having to deal with all the other frames

Alternatively is there a status flag that identifies the final frame, or is it safe to assume that the last frame that I receive notification of is the "real" site frame

I have also found that with both IE6 and IE7 I occasionally never receive a document complete event for the "real" frame, in other words I get a dozen or more ad-based frames and nothing else, and yet the site correctly loads in the browser.

I am using IHTMLDocument2::get_URL to obtain the URL at the document complete event.

Thanks!
Graeme



Answer this question

Document Complete event for multiple frames

  • JGttttt

    Thanks folks.

    I'm still finding that certain sites do not always deliver the parent frame to my OnDocumentComplete handler though. www.luxist.com is a case in point. It has about 6 ad frames and then the www.luxist.com frame itself. About 50% of the time I do not catch the www.luxist.com frame.

    Any ideas why this is flakey Is it an IE bug or me I wonder If someone has time perhaps they could try their own code on this site and see if they always get the parent frame

    Thanks,
    Graeme


  • Kartit

    Hi

    This method fails with the refresh (F5)! Try with http://www.microsoft.com/windows/ie/default.mspx or http://www.cnn.com/

    Does somebody knows the solution


  • _mAyDaY_

    In the DocumentComplete event, the first param IDispath is of the IWebBrowser2 where document was completed. When your BHO is instantiated you are given the toplevel IWebBrowser2 during IObjectWithSite::SetSite, so just check if the IWebBrowser2 passed in the DocumentComplete is the same as the one given to you at SetSite.
  • Werner Clausen

    Sharath is correct below is some code that might help.

    STDMETHODIMP CHelloWorld::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
    {

    HRESULT hr = S_OK;
    CComPtr<IDispatch> spDispDoc;
    CComPtr<IHTMLDocument2> spHTMLDoc;

    // Convert the IDispatch into IWebBrowser2
    CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;

    // Is this event associated with the top-level browser
    if (spTempWebBrowser && m_spWebBrowser && m_spWebBrowser.IsEqualObject(spTempWebBrowser))
    {

    // Gets document object from browser...
    hr = m_spWebBrowser->get_Document(&spDispDoc);
    if (SUCCEEDED(hr))
    {

    //...and verifies that it is an HTML document
    hr = spDispDoc->QueryInterface(IID_IHTMLDocument3,(void**)&spHTMLDoc);

    if (SUCCEEDED(hr))
    {

    // do something here
    }

    }
    return hr;

    }



  • bheybi

    Can any one please tell me what is m_spWebBrowser in the above code


  • wormbyte

    It is a (smart) pointer to the browser interface obtained in the SetSite function. I suggest you look at the BHO tutorial on MSDN which explains in detail.

  • coolcoder

    No, I meant there is no BeforeNavigate2/DocumentComplete for the main page, there would be BeforeNavigate2/DocumentComplete for subframes in the page. I created a small page which has the following -
    <html>
    <body>
    <iframe src="http://www.google.com"/>
    Some Tex for main page
    </body>
    </html>

    Now, when I load the document, I get a BeforeNavigate2/DocumentComplete twice (one for the main document & one for the iframe). But if I do a refresh, I get only one and its for the iframe and none of the main page. Regarding sample code for detecting the refresh, I had come across this on codeproject.com (http://www.codeproject.com/internet/detecting_the_ie_refresh.asp). But again these solutions are mainly heuristics based and guess works.

    HTH,
    Sharath


  • rodgerst

    No, for a refresh unfortunately there is no DocumentComplete/BeforeNavigate2 event raised. There are lot of heuristics based solutions out there to detect the refresh button being clicked in IE, but its something which definitely needs to be looked into for the future release of IE.

  • JRLiem

    to antonio : sorry ,I have not subroutine in VB

    chuncheng


  • Basiclife

    But, wich is the interest This method fails with the refresh.
  • LiveGadgets

    Jeremy Epling - MSFT wrote:

    Sharath is correct below is some code that might help.

    STDMETHODIMP CHelloWorld::OnDocumentComplete(IDispatch *pDisp, VARIANT *pvarURL)
    {

    HRESULT hr = S_OK;
    CComPtr<IDispatch> spDispDoc;
    CComPtr<IHTMLDocument2> spHTMLDoc;

    // Convert the IDispatch into IWebBrowser2
    CComQIPtr<IWebBrowser2> spTempWebBrowser = pDisp;

    // Is this event associated with the top-level browser
    if (spTempWebBrowser && m_spWebBrowser && m_spWebBrowser.IsEqualObject(spTempWebBrowser))
    {

    // Gets document object from browser...
    hr = m_spWebBrowser->get_Document(&spDispDoc);
    if (SUCCEEDED(hr))
    {

    //...and verifies that it is an HTML document
    hr = spDispDoc->QueryInterface(IID_IHTMLDocument3,(void**)&spHTMLDoc);

    if (SUCCEEDED(hr))
    {

    // do something here
    }

    }
    return hr;

    }

    in vc6 CHTMLVIEW::OnDocumentComplete(VARIANT *pvarURL) has only one parameter. Do I have to upgrade to vc7 or higt realse


  • dcohn

    Hi Chuncheng,

    Would you have this subroutine in Visual Basic

    Many thanks,

    Antonio


  • kryptonneke

    Thanks for your quick answer.

    But, Are you telling that the solution for 'Multiple DocumentComplete' just works until the refresh button is used

    How could I combine a solution for 'Multiple DocumentComplete' and a solution for the 'Refresh'

    Do you have a good sample solution for the refresh

    Thanks.


  • amitsinha

    OK, the bug seems to be on my side. I rewrote my comparison for the top level browser and it seems to work great now. I do always get the top level/parent frame.

    Thanks again,

    Graeme


  • Document Complete event for multiple frames