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

How to load HTML directly to a WebBrowser

By Zarko Gajic, About.com

Here's how to load "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

~~~~~~~~~~~~~~~~~~~~~~~~~
procedure WBLoadHTML(WebBrowser: TWebBrowser; HTMLCode: string) ;
var
   sl: TStringList;
   ms: TMemoryStream;
begin
   WebBrowser.Navigate('about:blank') ;
   while WebBrowser.ReadyState < READYSTATE_INTERACTIVE do
    Application.ProcessMessages;

   if Assigned(WebBrowser.Document) then
   begin
     sl := TStringList.Create;
     try
       ms := TMemoryStream.Create;
       try
         sl.Text := HTMLCode;
         sl.SaveToStream(ms) ;
         ms.Seek(0, 0) ;
         (WebBrowser.Document as IPersistStreamInit).Load(TStreamAdapter.Create(ms)) ;
       finally
         ms.Free;
       end;
     finally
       sl.Free;
     end;
   end;
end;

procedure TForm1.FormCreate(Sender: TObject) ;
var
  sHTML : string;
begin
  sHTML := '<a href="http://delphi.about.com">GOTO</a>' +
           '<b>About Delphi Programming</b>';
  WBLoadHTML(WebBrowser1,sHTML) ;
end;
~~~~~~~~~~~~~~~~~~~~~~~~~

Delphi tips navigator:
» How to draw transparent text on Windows Desktop.
« Enter key or Return key?

Zarko Gajic
Guide since 1998

Zarko Gajic
Delphi Programming Guide

Explore Delphi Programming
About.com Special Features

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

Easy ways to connect two computers for networking purposes. More >

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

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

All rights reserved.