How can i detect if an Iframe has loaded in C#

843 Views Asked by At

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;
    }
 }
1

There are 1 best solutions below

0
HackSlash On BEST ANSWER

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.