Check if an HTML element is displayed #69

A (X)HTML element is displayed only if its display attribute is not "none" and all of its parent elements are visible.

The above rules need make me think "recursion" so, I developed the following routine to check if a given element in a TWebBrowser control is displayed:

function THTMLDocHelper.ElemIsVisible(const Elem: IDispatch): Boolean;
var
  Element: IHTMLElement;  // IHTMLElement interface to Elem
begin
  if Supports(Elem, IHTMLElement, Element) then
  begin
    // Check if element itself is visible
    Result := (Element.style.display <> 'none');
    if Result and Assigned(Element.parentElement) then
      // Element is visible: check if parent is visible (recursive)
      Result := ElemIsVisible(Element.parentElement);
  end
  else
    // Not an HTML element
    Result := False;
end;

To use the function pass the IDispatch interface to the required element to it.

Author: Peter Johnson
Contributor: Peter Johnson
Added: 2008/11/26
Last updated: 2008/11/26