Link to home
Start Free TrialLog in
Avatar of JohnDoeSr
JohnDoeSr

asked on

iframe on webpage interfering with documentcomplete event

From what I understand, DocumentComplete fires when a page is done loading. However, if there is a frame on the page, it will ALSO fire when that frame is done loading. So if the main page hasn't finished loading yet but a frame has, DocumentComplete fires telling the code to continue on because the page is done loading and I'll end up getting errors.

The page in question has a single <iframe>. It looks like documentcomplete is firing when this iframe is done loading. There are no other <frame> tags on the page. Is there a way for me to just have documentcomplete fire when the main page is loading? Or some other solution that will prevent this premature firing or wait for 2 documentcompletes to fire.
Avatar of paulgrunner
paulgrunner

You can use a blocking while loop:

Dim objHTML as HTMLDocument
Set objHTML = ObjIE.Document


'This is the while loop. You can use DoEvents to allow processing windows messages:
While objHTML.readystate <> READYSTATE_COMPLETE
   DoEvents
Wend

How To Determine When a Page Is Done Loading in WebBrowser Control
http://support.microsoft.com/kb/q180366/
Avatar of JohnDoeSr

ASKER

Thanks for the replies. paulgrunner, is there any advantage to using objhtml.readystate versus explorer.readystate or webbrowser.readystate? I've already used the previous 2 in do loops and as soon as the first frame on the page is done loading, the documentcomplete event fires and readystate = readystate_complete even though the rest of the page isn't yet done.

Use the following code:

    Set objIE = New InternetExplorer
    objIE.Visible = True
    objIE.navigate "http://www.yourpage.com"
    While objIE.readyState <> READYSTATE_COMPLETE
        DoEvents
    Wend
    Dim objHTML As HTMLDocument
    Set objHTML = objIE.document
    While objHTML.readyState <> "complete"
        DoEvents
    Wend

Note that the first loop makes sure we can set the document object, while the second makes sure it's done.
Use this instead of the documentComplete event.
Hi Paul. In General Declarations i had to insert

Public objIE As SHDocVw.InternetExplorer

to get the IE aspect working right.

In Sub Form (load) I inserted the code you suggested above.
When hitting Play I get a compile error 'User-Defined type not defined error on the Dim objHTML as HTMLDocument line. If I omit from that line down then IE will launch successfully.

In the project I have Microsoft Internet Controls (shdocvw.dll) component added along with References to Microsoft Internet Controls (shdocvw.oca & shdocvw.dll), OLE Automation and runtime object produces/ vb for applications & vb objects and procedures.
ASKER CERTIFIED SOLUTION
Avatar of paulgrunner
paulgrunner

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial