Func _IELoadWait(ByRef $O_OBJECT, $I_DELAY = 0, $I_TIMEOUT = -1)
If Not IsObj($O_OBJECT) Then
__IEERRORNOTIFY("Error", "_IELoadWait", "$_IEStatus_InvalidDataType")
Return SetError($_IESTATUS_INVALIDDATATYPE, 1, 0)
EndIf
If Not __IEISOBJTYPE($O_OBJECT, "browserdom") Then
__IEERRORNOTIFY("Error", "_IELoadWait", "$_IEStatus_InvalidObjectType", ObjName($O_OBJECT))
Return SetError($_IESTATUS_INVALIDOBJECTTYPE, 1, 0)
EndIf
Local $OTEMP, $F_ABORT = False, $I_ERRORSTATUSCODE = $_IESTATUS_SUCCESS
Local $STATUS = __IEINTERNALERRORHANDLERREGISTER()
If Not $STATUS Then __IEERRORNOTIFY("Warning", "_IELoadWait", "Cannot register internal error handler, cannot trap COM errors", "Use _IEErrorHandlerRegister() to register a user error handler")
Local $F_NOTIFYSTATUS = _IEErrorNotify()
_IEErrorNotify(False)
Sleep($I_DELAY)
Local $IELOADWAITTIMER = TimerInit()
If $I_TIMEOUT = -1 Then $I_TIMEOUT = $__IELOADWAITTIMEOUT
Switch ObjName($O_OBJECT)
Case "IWebBrowser2"
While Not (String($O_OBJECT.readyState) = "complete" Or $O_OBJECT.readyState = 4 Or $F_ABORT)
If (TimerDiff($IELOADWAITTIMER) > $I_TIMEOUT) Then
$I_ERRORSTATUSCODE = $_IESTATUS_LOADWAITTIMEOUT
$F_ABORT = True
EndIf
If @error = $_IESTATUS_COMERROR And __IECOMERRORUNRECOVERABLE() Then
$I_ERRORSTATUSCODE = __IECOMERRORUNRECOVERABLE()
$F_ABORT = True
EndIf
Sleep(100)
WEnd
While Not (String($O_OBJECT.document.readyState) = "complete" Or $O_OBJECT.document.readyState = 4 Or $F_ABORT)
If (TimerDiff($IELOADWAITTIMER) > $I_TIMEOUT) Then
$I_ERRORSTATUSCODE = $_IESTATUS_LOADWAITTIMEOUT
$F_ABORT = True
EndIf
If @error = $_IESTATUS_COMERROR And __IECOMERRORUNRECOVERABLE() Then
$I_ERRORSTATUSCODE = __IECOMERRORUNRECOVERABLE()
$F_ABORT = True
EndIf
Sleep(100)
WEnd
Case "DispHTMLWindow2"
While Not (String($O_OBJECT.document.readyState) = "complete" Or $O_OBJECT.document.readyState = 4 Or $F_ABORT)
If (TimerDiff($IELOADWAITTIMER) > $I_TIMEOUT) Then
$I_ERRORSTATUSCODE = $_IESTATUS_LOADWAITTIMEOUT
$F_ABORT = True
EndIf
If @error = $_IESTATUS_COMERROR And __IECOMERRORUNRECOVERABLE() Then
$I_ERRORSTATUSCODE = __IECOMERRORUNRECOVERABLE()
$F_ABORT = True
EndIf
Sleep(100)
WEnd
While Not (String($O_OBJECT.top.document.readyState) = "complete" Or $O_OBJECT.top.document.readyState = 4 Or $F_ABORT)
If (TimerDiff($IELOADWAITTIMER) > $I_TIMEOUT) Then
$I_ERRORSTATUSCODE = $_IESTATUS_LOADWAITTIMEOUT
$F_ABORT = True
EndIf
If @error = $_IESTATUS_COMERROR And __IECOMERRORUNRECOVERABLE() Then
$I_ERRORSTATUSCODE = __IECOMERRORUNRECOVERABLE()
$F_ABORT = True
EndIf
Sleep(100)
WEnd
Case "DispHTMLDocument"
$OTEMP = $O_OBJECT.parentWindow
While Not (String($OTEMP.document.readyState) = "complete" Or $OTEMP.document.readyState = 4 Or $F_ABORT)
If (TimerDiff($IELOADWAITTIMER) > $I_TIMEOUT) Then
$I_ERRORSTATUSCODE = $_IESTATUS_LOADWAITTIMEOUT
$F_ABORT = True
EndIf
If @error = $_IESTATUS_COMERROR And __IECOMERRORUNRECOVERABLE() Then
$I_ERRORSTATUSCODE = __IECOMERRORUNRECOVERABLE()
$F_ABORT = True
EndIf
Sleep(100)
WEnd
While Not (String($OTEMP.top.document.readyState) = "complete" Or $OTEMP.top.document.readyState = 4 Or $F_ABORT)
If (TimerDiff($IELOADWAITTIMER) > $I_TIMEOUT) Then
$I_ERRORSTATUSCODE = $_IESTATUS_LOADWAITTIMEOUT
$F_ABORT = True
EndIf
If @error = $_IESTATUS_COMERROR And __IECOMERRORUNRECOVERABLE() Then
$I_ERRORSTATUSCODE = __IECOMERRORUNRECOVERABLE()
$F_ABORT = True
EndIf
Sleep(100)
WEnd
Case Else
$OTEMP = $O_OBJECT.document.parentWindow
While Not (String($OTEMP.document.readyState) = "complete" Or $OTEMP.document.readyState = 4 Or $F_ABORT)
If (TimerDiff($IELOADWAITTIMER) > $I_TIMEOUT) Then
$I_ERRORSTATUSCODE = $_IESTATUS_LOADWAITTIMEOUT
$F_ABORT = True
EndIf
If @error = $_IESTATUS_COMERROR And __IECOMERRORUNRECOVERABLE() Then
$I_ERRORSTATUSCODE = __IECOMERRORUNRECOVERABLE()
$F_ABORT = True
EndIf
Sleep(100)
WEnd
While Not (String($OTEMP.top.document.readyState) = "complete" Or $O_OBJECT.top.document.readyState = 4 Or $F_ABORT)
If (TimerDiff($IELOADWAITTIMER) > $I_TIMEOUT) Then
$I_ERRORSTATUSCODE = $_IESTATUS_LOADWAITTIMEOUT
$F_ABORT = True
EndIf
If @error = $_IESTATUS_COMERROR And __IECOMERRORUNRECOVERABLE() Then
$I_ERRORSTATUSCODE = __IECOMERRORUNRECOVERABLE()
$F_ABORT = True
EndIf
Sleep(100)
WEnd
EndSwitch
_IEErrorNotify($F_NOTIFYSTATUS)
__IEINTERNALERRORHANDLERDEREGISTER()
Switch $I_ERRORSTATUSCODE
Case $_IESTATUS_SUCCESS
Return SetError($_IESTATUS_SUCCESS, 0, 1)
Case $_IESTATUS_LOADWAITTIMEOUT
__IEERRORNOTIFY("Warning", "_IELoadWait", "$_IEStatus_LoadWaitTimeout")
Return SetError($_IESTATUS_LOADWAITTIMEOUT, 3, 0)
Case $_IESTATUS_ACCESSISDENIED
__IEERRORNOTIFY("Warning", "_IELoadWait", "$_IEStatus_AccessIsDenied", "Cannot verify readyState. Likely casue: cross-site scripting security restriction.")
Return SetError($_IESTATUS_ACCESSISDENIED, 0, 0)
Case $_IESTATUS_CLIENTDISCONNECTED
__IEERRORNOTIFY("Error", "_IELoadWait", "$_IEStatus_ClientDisconnected", "Browser has been deleted prior to operation.")
Return SetError($_IESTATUS_CLIENTDISCONNECTED, 0, 0)
Case Else
__IEERRORNOTIFY("Error", "_IELoadWait", "$_IEStatus_GeneralError", "Invalid Error Status - Notify IE.au3 developer")
Return SetError($_IESTATUS_GENERALERROR, 0, 0)
EndSwitch
EndFunc