Notice Of Closure

Please note that this website is due to close down on 15th June 2020.

In the interim I will try to move as much content as possible over to my GitHub account. Thanks to everyone who has supported the site over the years.

» Help

How to extract the title of an HTML document


Suppose you're writing a web browser using the Internet Explorer browser control and you want to display the title of the document in your program's title bar. You will need to be able to extract the loaded document's title from the HTML document itself. Here's how.

Here's a function that will read the title from a HTML document.

function GetHTMLDocTitle(const Doc: IDispatch): string;
  if Supports(Doc, IHTMLDocument2) then
    Result := (Doc as IHTMLDocument2).title
    Result := '';

Assuming we have have dropped a TWebBrowser control onto a Delphi form named Form1, we set the form's caption like this:

Form1.Caption := GetHTMLDocTitle(WebBrowser1.Document);

Note that this code assumes the required HTML document is already loaded into the browser control and that it has finished loading.

See Tip #72 to learn how to ensure the HTML document has loaded.

Author: Peter Johnson
Contributor: Peter Johnson
Added: 2009-06-17
Last updated: 2009-06-17

« Return to contents »