6

I want to prevent iframe elements from triggering the OnDocumentComplete event every time. For example, a page has 4 iframes, and when I load this page, my OnDocumentComplete event runs 4 times. I want to run OnDocumentComplete just once for every page. How can I do that?

Maybe I could remove or block iframes in TWebBrowser control.

Rob Kennedy
  • 161,384
  • 21
  • 275
  • 467
Someone
  • 728
  • 2
  • 12
  • 23
  • I hope you mean hide, not remove. So [`here`](http://www.delphidabbler.com/tips/56) you have how to find an element and on the found element try to set `Element.Style := 'visibility:hidden;'`. – TLama Apr 11 '12 at 11:52
  • Actually I want to prevent them from run OnDocumentComplete event everytime. For example; page has 4 iframe and when i load this page OnDocumentComplete event runs 4 times. I want to run OnDocumentComplete just once for every page. – Someone Apr 11 '12 at 11:59

1 Answers1

14

The event OnDocumentComplete is fired for each FRAME/IFRAME in the main document.
If you want to ignore them try this:

procedure TForm1.WebBrowser1DocumentComplete(Sender: TObject;
  const pDisp: IDispatch; var URL: OleVariant);
begin
  // check that the event is raised for the top-level browser (not frames or iframes)
  if pDisp = TWebBrowser(Sender).ControlInterface then
  begin
    // do something nice...
  end;
end;

From Delphi Docs:

Write an OnDocumentComplete event handler to take specific action when a frame or document is fully loaded into the Web browser. For a document without frames, this event occurs once when the document finishes loading. On a document containing multiple frames, this event occurs once for each frame. When the multiple-frame document finishes loading, the Web browser fires the event one final time.

Sender is the Web browser that is loading the document.

pDisp is the Automation interface of the top-level frame or browser. When loading a document without frames, pDisp is the interface of the Web browser. When loading a document with multiple frames, this is the interface of the containing frame, except for the very last time the event occurs, when it is the interface of the Web browser.

kobik
  • 21,001
  • 4
  • 61
  • 121
  • 1
    Would be a nice utility function `InterfaceCompare(x:IDispatch;aObject:TObject)` that wraps that big IF statement. – Warren P Apr 11 '12 at 17:38
  • 1
    @WarrenP, Actually, `if pDisp = TWebBrowser(Sender).ControlInterface then...` will do just fine. Thanks for your comment. – kobik Apr 11 '12 at 19:01
  • Oh that's great. Maybe a utility function might catch some extraordinary situation (something not assigned or other corner cases?) – Warren P Apr 11 '12 at 19:17