6

I have a TWebBrowser object which is created in runtime and used in background, that is, not visible. The problem is that events like OnDocumentComplete dont work or are not triggered in Delphi2009. Any advice?

procedure TfrmMain.FormCreate(Sender: TObject);
begin
  FWebBrowser:= TWebBrowser.Create(Self);
  FWebBrowser.RegisterAsBrowser:= True;
  FWebBrowser.OnDocumentComplete:= WhenDocIsCompleted;
end;

procedure TfrmMain.WhenDocIsCompleted(ASender: TObject; const pDisp: IDispatch;
  var URL: OleVariant);
begin
  ShowMessage('Doc is completed!');
end;

There is any difference important between Navigate and Navigate2? How can I enable cookies here?

Thanks in advance.

Billiardo Aragorn
  • 347
  • 1
  • 4
  • 16
  • 1
    Not really an answer to your question, but why would you use TWebBrowser if you are not using it for display? Wouldn't be better to just get it using a HTTP Client component like Indy's TIdHTTP or the WinInet API that uses the same settings as IE. See http://stackoverflow.com/questions/1823542/how-to-send-a-http-post-request-in-delphi-using-wininet-api – Lars Truijens Dec 27 '09 at 11:36
  • 1
    I've used TidHTTP and TidCokkieManager but problems with cookies persist so I saw TWebBrowser like a solution but after tests it has same problem. – Billiardo Aragorn Dec 27 '09 at 16:26
  • So what is the question? Is it about the cookies or the event? Maybe it is best if you separate both questions. – Lars Truijens Dec 27 '09 at 18:26

5 Answers5

7
TWinControl(FWebBrowser).Parent := Form1;  // Parent property is read-only unless cast
perror
  • 7,071
  • 16
  • 58
  • 85
tomo7
  • 451
  • 7
  • 6
2

You may have this issue because the TWebBrowser internally works closely together with the handle of the parent form to get messages posted from windows. Try using a hidden form with the TWebBrowser on (optionally run-time created as well), and/or investigate if the HandleAllocated and HandleNeeded methods could help you.

Stijn Sanders
  • 35,982
  • 11
  • 45
  • 67
2

Call for the OnDocumentComplete Problem:

WebBrowser1.HandleNeeded;

or in your case:

FWebBrowser.HandleNeeded;

before webBrowser.Navigate

Ingo
  • 5,239
  • 1
  • 30
  • 24
1
procedure TForm1.ReCreateBrowser();
begin
    if(WebBrowser <> NIL) then
    begin
       WebBrowser.Stop;
       WebBrowser.Destroy;
    end;

    WebBrowser        := TWebBrowser.Create(Form1);
    TWinControl(WebBrowser).Name   := 'WebBrowser';
    TWinControl(WebBrowser).Parent := Form1; //set parent...can be panel, tabs etc
    WebBrowser.Silent := true;  //don't show JS errors
    WebBrowser.Visible:= true;  //visible...by default true

    //don't set width/heigh/top/left before TWinControl(WebBrowser).Parent := Form1;
    WebBrowser.Top    := 10;
    WebBrowser.Left   := 10;
    WebBrowser.Height := 600;
    WebBrowser.Width  := 800;
    WebBrowser.OnDocumentComplete  := WebBrowserDocumentComplete;
  //WebBrowser.OnNavigateComplete2 := WebBrowserNavigateComplete2;
end;
1

A component working perfectly with web-pages cookies is TEmbeddedWB from EmbeddedWB and is free.

Billiardo Aragorn
  • 347
  • 1
  • 4
  • 16
  • I cant access EmbeddedWB website for long time. I have the sources and Im using in Delphi 2010, also I have improved some parts and fixed some bugs. – Cesar Romero Dec 29 '09 at 19:52
  • For the "latest" downloads see [Wayback's archive](https://web.archive.org/web/*/http://www.bsalsa.com/Downloads/*), since the website ceased to exist after 2009-04. – AmigoJack Nov 30 '22 at 10:25