DelphiFAQ Home Search:
General :: Programming :: Delphi :: Network
Network (local network and Internet) programming under Windows with Delphi.


This list is sorted by recent document popularity (not total page views).
New documents will first appear at the bottom.

Featured Article

TWebBrowser: Determine when a page with Frames is completed


If I load a web page with TWebBrowser that contains frames then the OnDocumentComplete() is hit for each frame. How can I recognize that the page is completely loaded (no more frames missing)?


Indeed, in case of multiple frames, OnDocumentComplete gets fired multiple times. Not every frame fires this event, but each frame that fires a DownloadBegin event will fire a corresponding DocumentComplete event.

How can the 'real completion' be recognized?

The OnDocumentComplete event sends parameter pDisp: IDispatch, which is the IDispatch of the frame (shdocvw) for which DocumentComplete is fired. The top-level frame fires the DocumentComplete in the end.

So, to check if a page is done downloading, you need to check if pDisp is same as the IDispatch of the WebBrowser control.

That's what the code below demonstrates.

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
                const pDisp: IDispatch; var URL: OLEvariant);
   CurWebrowser : IWebBrowser;
   TopWebBrowser: IWebBrowser;
   Document     : OLEvariant;
   WindowName   : string;
 begin { TForm1.WebBrowser1DocumentComplete }
   CurWebrowser := pDisp as IWebBrowser; 
   TopWebBrowser := (Sender as TWebBrowser).DefaultInterface; 
   if CurWebrowser=TopWebBrowser then 
     ShowMessage('Document is complete.') 
     Document := CurWebrowser.Document; 
     WindowName := Document.ParentWindow.Name; 
     ShowMessage('Frame ' + WindowName + ' is loaded.')
You don't like the formatting? Check out SourceCoder then!
Generated 0:01:44 on May 26, 2017