DelphiFAQ Home Search:

TWebBrowser: Determine when a page with Frames is completed

 

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

Question:

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)?

Answer:

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);
var
  CurWebrowser : IWebBrowser;
  TopWebBrowser: IWebBrowser;
  Document     : OLEvariant;
  WindowName   : string;
begin { TForm1.WebBrowser1DocumentComplete }
  CurWebrowser := pDisp as IWebBrowser; 
  TopWebBrowser := (Sender as TWebBrowser).DefaultInterface; 
  if CurWebrowser=TopWebBrowser then 
  begin
    ShowMessage('Document is complete.') 
  end
  else 
  begin 
    Document := CurWebrowser.Document; 
    WindowName := Document.ParentWindow.Name; 
    ShowMessage('Frame ' + WindowName + ' is loaded.')
  end;
end;
You don't like the formatting? Check out SourceCoder then!
Content-type: text/html

Comments:

2006-04-10, 07:33:55
anonymous from Germany  
rating
THANKS IT HELPED ME ALOT!
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
chperetti@infomaniak.ch from France  
rating
Thanks you for this good code and explanations
2009-11-15, 15:40:18
anonymous from United States  
rating
Excellent help!

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

2011-11-21, 12:36:31
anonymous  
ok
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

 

 

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: