DelphiFAQ Home Search:

TWebBrowser: Determine when a page with Frames is completed


comments10 comments. Current rating: 3 stars (7 votes). Leave comments and/ or rate it.


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!


2006-04-10, 07:33:55
anonymous from Germany  
2006-04-10, 22:55:51
anonymous from Russian Federation  
Great solution, but how to Fire Event For each Frame befoure it will be shown ?
It seems me that all frames are fired parralel and I cannot remove for example Exploits from there dynamicaly.

If I do it with OnNavigateComplete event it is fired after main document or Frame is loaded. But frame goes on loading when it is fired and Javascripts are runned befoure I can filter them from event.
2006-04-10, 23:34:57
anonymous from Russian Federation  
May be there is any other simple way to Filter all incoming traffic to TWebBrowser of TEmbeddedWB and it's Frames
and remove any bad Javascripts from there ?
2009-03-16, 11:14:55 from France  
Thanks you for this good code and explanations
2009-11-15, 15:40:18
anonymous from United States  
Excellent help!

Thanks for the tips, what would we do without the Internet!

2011-11-21, 12:36:31
this is good code
but there are some problem
if ur broser don't find the page of your url it will dowload the error page defult
this code can not detect that page is error page ok
2018-12-22, 08:18:36
good to know this easy solution
2020-06-27, 13:33:14
Reginald from Greece  
buy online cialis pharmacy 20mg
2020-06-30, 13:07:09
Raymond from Greece  
bimatoprost generic india
2020-07-04, 05:21:05
is it legal to buy cialis online



NEW: Optional: Register   Login
Email address (not necessary):

Rate as
Hide my email when showing my comment.
Please notify me once a day about new comments on this topic.
Please provide a valid email address if you select this option, or post under a registered account.

Show city and country
Show country only
Hide my location
You can mark text as 'quoted' by putting [quote] .. [/quote] around it.
Please type in the code:

Please do not post inappropriate pictures. Inappropriate pictures include pictures of minors and nudity.
The owner of this web site reserves the right to delete such material.

photo Add a picture: