How to attach an event to IHTMLDocument2 link elements in Delphi?

Posted by Sebastian on Stack Overflow See other posts from Stack Overflow or by Sebastian
Published on 2009-12-02T02:26:24Z Indexed on 2010/03/29 19:13 UTC
Read the original article Hit count: 740

I'm using this code to get all the links from an IHTMLDocument2:

procedure DoDocumentComplete(const pDisp: IDispatch; var URL: OleVariant);
var
  Document:IHTMLDocument2;
  Body:IHTMLElement;
  Links:IHTMLElementCollection;
  i:integer;
  tmp:IHTMLElement;
begin
  try
  Document := (pDisp as  IWebbrowser2).Document AS IHTMLDocument2;
  Body := Document.body;
  Links := Document.links;
  for i := 0 to (Links.length-1) do
    begin
      tmp := (Links.item(i, 0) as IHTMLElement);
      //tmp.onclick := HOW SHOULD I ADD THE CALLBACK HERE?
      //ShowMessage(tmp.innerText);
    end;
  except
    on E : Exception do
      ShowMessage(E.ClassName+' error raised, with message : '+E.Message);
  end;
end;

How could I attach a function/procedure to .onclick to do a simple task like show an alert with the anchor text when the link is clicked?

© Stack Overflow or respective owner

Related posts about delphi

Related posts about ihtmldocument2