1

I want identify in an HTML document whether an Iframe has loaded or not.

Here is what I tried, but I am pretty not sure if this is the right way to identify the loading of IFrame:

 var myhtmlDocument = (IHTMLDocument2) HtmlDocument.DomDocument;
 IHTMLSelectionObject htmlselectionObject = myhtmlDocument.selection;
 bool frameloaded = false;
 if (htmlselectionObject != null)
  {
    FramesCollection frames = myhtmlDocument.frames;
    IHTMLSelectionObject frameSelection = frame.document.selection;
    for (int i = 0; i < frames.length; i++)
    {
       object index = i;
       IHTMLWindow2 frame = (IHTMLWindow2) frames.item(ref index);                                
       IHTMLSelectionObject frameSelection = frame.document.selection;

       if(frameSelection)
          frameloaded = true;
    }
 }
DotNetSpartan
  • 931
  • 4
  • 20
  • 41
  • 2
    I haven't needed to do this in decades but back then we were hooking to a `onload` event on the `iframe` and had to check if it contain an error message if it did eventually load. I don't know if that is still relevant these days – Franck Sep 22 '20 at 12:05

1 Answers1

1

The object model you are using is deprecated because it's based on Internet Explorer.

If you're able to switch to using the modern WebView2 (Edge based) object then you can listen for the CoreWebView2.NavigationCompleted Event

If you're really stuck with IHTMLDocument2 then you would have to do a similar thing with addEventListener

IHTMLWindow2 frame = (IHTMLWindow2) frames.item(ref index); 
HRESULT retVal = frame.document.event.addEventListener("load", listener, true);

Where listener is a compatible event handler that will act upon the page load event.

HackSlash
  • 4,944
  • 2
  • 18
  • 44