DelphiFAQ Home Search:

TWebBrowser: Determine when a page with Frames is completed

 

comments3 comments. Current rating: 5 stars (1 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!

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 ?

 

 

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.
 
It seems that you are
from Washington, US .

Info/ Feedback on this

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:
photo Add a picture:

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.