0

I use TWebBrowser in Delphi 10.x, I want to navigate to local file like index.html that will load some html/js/css, it is working and loaded successfully. Now I wanted to add to the body some html text, but it is not working, nothing happened (without raising errors).

//Navigate to local file
   WebBrowser.Navigate(f);
//Writing a string to body
   with WebBrowser.Document as IHTMLDocument2 do
   begin
      WebBody := body;
      WebBody.insertAdjacentHTML('BeforeEnd', MyHTML);
   end;

If i do not Navigate to a local file but write a whole HTML as string to it,

    Navigate('about:blank', '', '', '', 'X-UA-Compatible: IE=edge,chrome=1');
   ...//write a initial html like above

then adding text by WebBody.insertAdjacentHTML, it is work fine.

How can I navigate local file then add some text to body (let assume it is chat app).

Ilyes
  • 14,640
  • 4
  • 29
  • 55
  • 2
    Hi Zaher, you must inject the html at the right moment. The best event to do this is `OnDocumentComplete` – whosrdaddy Apr 29 '19 at 13:22
  • Here is an example on [how to implement `OnDocumentComplete`](https://stackoverflow.com/questions/17999392/automated-log-in-webbrowser/18011171#18011171) – whosrdaddy Apr 29 '19 at 15:25

1 Answers1

0

I found the solution depend on @whosrdaddy comment above but not using OnDocumentComplete.

I should wait browser to complete the navigating/processing it

procedure WaitComplete; 
begin
    with WebBrowser do
      while Busy or (ReadyState <> READYSTATE_COMPLETE) do
      begin
        Vcl.Forms.Application.ProcessMessages;
      end;
end;

WebBrowser.Navigate(f);
WaitComplete;