onreadystatechange Event

 

Fires when the readyState method changes.

Inline HTML

<ELEMENT onreadystatechange = "handler" ... >

All platforms

Event property

object.onreadystatechange = handler

Microsoft® JScript® (compatible with ECMA 262 language specification) only

Named script

<SCRIPT FOR = object EVENT = onreadystatechange>

Microsoft Internet Explorer only

oXMLDOMDocument.onreadystatechange;

The following JScript and HTML example demonstrates the sequence of ready states when asynchronously loading a document.

<script>
var xmldoc;
function Load()
{
  xmldoc = new ActiveXObject("Msxml2.DOMDocument.6.0");
  xmldoc.onreadystatechange = CheckState;
  xmldoc.load(URL.value);
}
function CheckState()
{
  var state = xmldoc.readyState;
  RESULTS.innerHTML += "readyState = " + state + "<BR>"
  if (state == 4)
  {
    var err = xmldoc.parseError;
    if (err.errorCode != 0)
      RESULTS.innerHTML += err.reason + "<BR>"
    else RESULTS.innerHTML +="success" + "<BR>"
  }
}
</script>
URL: <input type=text size=60 id=URL>
<input type=button value=LOAD onclick="jscript:Load()">
<div id=RESULTS style= "color:red;font-weight:bold;"></div>

HRESULT onreadystatechange(void);
C/C++ Return Values:

S_OK

The value returned if successful.

BEGIN_DISPATCH_MAP(CXMLDOMSamples, CCmdTarget)
   DISP_FUNCTION_ID(CXMLDOMSamples, "Ready State Event Handler", 
   DISPID_XMLDOMEVENT_ONREADYSTATECHANGE, DOMDocCheckState, VT_EMPTY, 0)
END_DISPATCH_MAP()

class CXMLDOMSamples : public CCmdTarget
{
private:
   HRESULT m_hr;
   IXMLDOMDocument* m_pIXMLDOMDocument;
   BSTR m_bstrXMLDocName;
   DWORD m_pdwCookie;
public:
   BOOL AdviseConnectionPoint(BOOL bAdvise = TRUE);
   void Caller();
   BOOL DOMDocLoadLocation();
   BOOL DOMDocStateChangeSetup();
   void DOMDocCheckState();
};

// Assumptions   
// m_pIXMLDOMDocument has already been initialized (a DOMDocument 
// object has been created).
// Automation is enabled for the class to enable it to connect 
// to the Connection point (DIID_XMLDOMDocumentEvents).

void CXMLDOMSamples::Caller()
{
   try
   {
   m_bstrXMLDocName = ::SysAllocString(_T("http://MyServer/sample.xml"));
   DOMDocStateChangeSetup();
   DOMDocLoadLocation();
   ::SysFreeString(m_bstrXMLDocName);
   m_bstrXMLDocName = NULL;
}
   catch(...)
   {
      if(m_bstrXMLDocName)
      ::SysFreeString(m_bstrXMLDocName);
      DisplayErrorToUser();
   } 
}
BOOL CXMLDOMSamples::AdviseConnectionPoint(BOOL bAdvise)
{
   BOOL bResult = FALSE;
   try
   {
      IConnectionPointContainer *pIConnectionPointContainer = NULL;
      IConnectionPoint *pCP = NULL;

      m_hr = m_pIXMLDOMDocument->QueryInterface    
      (IID_IConnectionPointContainer,(void**)&pIConnectionPointContainer);
      SUCCEEDED(m_hr) ? 0 : throw m_hr;

      If(pIConnectionPointContainer)
      {
         m_hr = pIConnectionPointContainer->FindConnectionPoint 
               (DIID_XMLDOMDocumentEvents, &pCP);
         SUCCEEDED(m_hr) ? 0 : throw m_hr;
         if(SUCCEEDED(m_hr) && pCP)
         {
            if(bAdvise)
               m_hr = pCP->Advise(GetIDispatch(TRUE), &m_pdwCookie);
            else
               m_hr = pCP->Unadvise(m_pdwCookie);
            bResult = SUCCEEDED(m_hr) ? TRUE : FALSE;
            pCP->Release();
            pCP = NULL;
         }
         pIConnectionPointContainer->Release();
         pIConnectionPointContainer = NULL;
      }
   }
   catch(...)
   {
      if(pIConnectionPointContainer)
         pIConnectionPointContainer->Release();
      if(pCP)
         pCP->Release();
      DisplayErrorToUser();
   }
   return bResult;
}
BOOL CXMLDOMSamples::DOMDocStateChangeSetup()
{
   BOOL bResult = FALSE;
   try
   {
      m_hr = m_pIXMLDOMDocument->put_async(VARIANT_TRUE);
      SUCCEEDED(m_hr) ? 0 : throw m_hr;
      bResult = AdviseConnectionPoint();
   }
   catch(...)
   {
      DisplayErrorToUser();
   }
   return bResult;
}
BOOL CXMLDOMSamples::DOMDocLoadLocation()
{
   short sResult = FALSE;
   BOOL bResult = FALSE;
   try
   {
      _variant_t varString = m_bstrXMLDocName;
      m_hr = m_pIXMLDOMDocument->load(varString, &sResult);
      bResult = SUCCEEDED(m_hr) ? TRUE : FALSE;
   }
   catch(...)
   {
      DisplayErrorToUser();
   }
   return bResult;
}
void CXMLDOMSamples::DOMDocCheckState()
{
   IXMLDOMParseError *pIParseError = NULL;
   long value;

   try
   {
      m_hr = m_pIXMLDOMDocument->get_readyState(&value);
      SUCCEEDED(m_hr) ? 0 : throw m_hr;
      if(value == 4 )
      {
         m_hr = m_pIXMLDOMDocument->get_parseError(&pIParseError);
         SUCCEEDED(m_hr) ? 0 : throw m_hr;
         m_hr = pIParseError->get_errorCode(&value);
         pIParseError->Release();
         pIParseError=NULL;
         if(!SUCCEEDED(m_hr))
         {
            throw m_hr;
         }
         m_strOutput = (value != 0) ? _T("Load Error") : _T("Load 
            Succeeded");
      }
   }
   catch(...)
   {
      if(pIParseError)
         pIParseError->Release();
      DisplayErrorToUser();
   }
}

This event can be explicitly set using the onreadystatechange property.

This member is an extension of the Worldwide Web Consortium (W3C) Document Object Model (DOM).

Implemented in: MSXML 3.0 and MSXML 6.0

Show: