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

How to append HTML directly to a WebBrowser document

By , About.com Guide

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
About.com Special Features

Holiday Central

What to eat, where to go, fun things to do and how to save money on the perfect gifts. More >

Family Tech Center

Stay connected and entertained with reviews on tips on the latest HDTVs, cellphones and more. More >

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

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

All rights reserved.