«« Site Rescued »»

While the site is still going to move from its current host, a new site will now take its place. More Info.

The new site may have less content, but the core will now remain. And it will now play nicely with phones! Keep an eye on the DelphiDabbler Blog for news.

» 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 »