1. Home
  2. Computing & Technology
  3. Delphi Programming

How to append HTML directly to a WebBrowser document

By Zarko Gajic, About.com

Here's how to append (add) "static" HML code from a string into a TWebBrowser:

Usage: Simply drop an instance of TWebBrowser component on a form, HTML code gets loaded in the OnCreate event for a form. The Button's click event handler appends a link to About Delphi Programming web site at the bottom of the document

~~~~~~~~~~~~~~~~~~~~~~~~~
uses MSHTML;

procedure AppendToWB(WB: TWebBrowser; const html: widestring) ;
var
   Range: IHTMLTxtRange;
begin
   Range := ((WB.Document AS IHTMLDocument2).body AS IHTMLBodyElement).createTextRange;
   Range.Collapse(False) ;
   Range.PasteHTML(html) ;
end;

procedure TForm1.FormCreate(Sender: TObject) ;
begin
   WebBrowser1.Navigate('http://aspxDelphi.net') ;
end;

procedure TForm1.Button1Click(Sender: TObject) ;
var
   s: string;
begin
   s:= '<a href="http://delphi.about.com">go to About Delphi Programming</a>';
   AppendToWB(WebBrowser1,s) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» Sending email messages in .Net
« Simulating keystrokes from code

Explore Delphi Programming

More from About.com

  1. Home
  2. Computing & Technology
  3. Delphi Programming
  4. Using VCL Components
  5. TWebBrowser
  6. How to append HTML directly to a WebBrowser document

©2008 About.com, a part of The New York Times Company.

All rights reserved.