2

How to know if the TWebBrowser already finished to download the page? My problem is: I can't know when my page was completely downloaded so it can be shown.

I request one page to my webbrowser and I want to show the response only when the page was completely downloaded.

Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
RafaelPlantard
  • 117
  • 1
  • 7
  • 1
    Given dynamic nature of pages today (any page might be a self-modifying program, deciding it consists of yet more files at any random minute after any user actions or comet/ajax traffic) I don't think it is possible in general sense – Arioch 'The Jan 18 '14 at 15:16

2 Answers2

4

You could try handling the OnDocumentComplete event.

If the site uses scripting to trigger downloading of additional data, you may have to employ more sophisticated methods since the event will fire before the page finishes running all its scripts. In general, the task begins to look like the halting problem. You might wish to refine your definition of "completely downloaded" to exclude certain difficult-to-detect cases.

Community
  • 1
  • 1
Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
  • 2
    You did not mentioned frames. These are treated as seperate documents and start loading after the main document is completed – Sir Rufo Jan 18 '14 at 16:44
  • 4
    Each frame triggers its own OnDocumentComplete event, and then the main page triggers its OnDocumentComplete event at the end. – Remy Lebeau Jan 18 '14 at 17:07
1

source: http://www.delphifaq.com/faq/delphi/network/f264.shtml

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 IForm1.WebBrowser1Documentccmplete(Sender: Iobject:
const pDisp: Inispatch; var URL: OLEvariant):
var
Curwebrowser : IWebBrowser:
IopWebBrowser: IWebBrowser:
Document : OLEvariant;
WindowName : string:
begin { TForm1.WebBrowser1DocumentComplete }
Curwebrowser := pDisp as IWebBrowser:
TopWebBrowser := (Sender as IWebBrowser).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;
legoscia
  • 39,593
  • 22
  • 116
  • 167