How do I avoid the OnDocumentComplete event for embedded iframe elements?
Asked Answered
P

1

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.

Pyrrha answered 11/4, 2012 at 11:38 Comment(2)
I hope you mean hide, not remove. So here you have how to find an element and on the found element try to set Element.Style := 'visibility:hidden;'.Countrywide
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.Pyrrha
F
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.

Ferminafermion answered 11/4, 2012 at 12:13 Comment(3)
Would be a nice utility function InterfaceCompare(x:IDispatch;aObject:TObject) that wraps that big IF statement.Embryology
@WarrenP, Actually, if pDisp = TWebBrowser(Sender).ControlInterface then... will do just fine. Thanks for your comment.Ferminafermion
Oh that's great. Maybe a utility function might catch some extraordinary situation (something not assigned or other corner cases?)Embryology

© 2022 - 2024 — McMap. All rights reserved.