onreadystatechange Property (IXMLHTTPRequest)

MSXML 5.0 SDK

Microsoft XML Core Services (MSXML) 5.0 for Microsoft Office - DOM Reference

onreadystatechange Property (IXMLHTTPRequest)

Specifies the event handler to be called when the readyState property changes. Note that onreadystatechange is designed for use in scripting environments and is not readily accessible in Microsoft® Visual Basic® or C++.

[Script]

Script Syntax

oXMLHttpRequest.onreadystatechange = funcMyHandler;

Example

The following script example specifies the handler HandleStateChange gets called when an IXMLHTTPRequest object's readyState property changes. A button on a page is enabled when the readyState property indicates that all data has been received (readystate == 4).

<script>
var xmlhttp=null;
function PostOrder(xmldoc)
{
  var xmlhttp = new ActiveXObject5.0("Msxml2.XMLHTTP.5.0");
  xmlhttp.Open("POST", "http://myserver/orders/processorder.asp", false); 
  xmlhttp.onreadystatechange= HandleStateChange;
  xmlhttp.Send(xmldoc);
  myButton.disabled = true;
}
function HandleStateChange()
{
  if (xmlhttp.readyState == 4)
  {
    myButton.disabled = false;
    alert("Result = " + xmlhttp.responseXML.xml);
  }
}
</script>

In Microsoft Visual Basic® Scripting Edition (VBScript), you can get a function pointer using the syntax getRef("HandleStateChange").

[Visual Basic]

Visual Basic Syntax

In Visual Basic, you need to write the following statement where xmldoc is a variable and implements a subroutine called xmldoc_onreadystatechange().

Dim WithEvents xmldoc As DOMDocument50

Example

See Use OnReadyStateChange Property in Visual Basic and Visual C/C++.

[C/C++]

C/C++ Syntax

Use connection points to trap readystatechange events.

The following macros declare the handler (DOMDocCheckState) for the event:

BEGIN_DISPATCH_MAP(CXMLDOMSamples, CCmdTarget)
   DISP_FUNCTION_ID(CXMLDOMSamples, "Ready State Event Handler", 
   DISPID_XMLDOMEVENT_ONREADYSTATECHANGE, DOMDocCheckState, VT_EMPTY, 0)
END_DISPATCH_MAP()
“m_hr = pCP->Advise(GetIDispatch(TRUE), &m_pdwCookie); “ 

The last line registers this class (CXMLDOMSamples) as an event listener with the document Connection Point Container. 

Parameters

pReadyStateSink [in]
Name of the function that should be called when the readyState property value changes.

C/C++ Return Values

S_OK
Value returned if successful.

Example

See Use OnReadyStateChange Property in Visual Basic and Visual C/C++.

Remarks

The property is write-only. When using scripting languages, this property can be set in ways other than directly accessing the property through IXMLHTTPRequest. It can also be set using the onreadystatechange attribute of the <XML> tag, and the SCRIPT FOR... construct.

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

To view reference information for Visual Basic, C/C++, or Script only, click the Language Filter button Language Filter in the upper-left corner of the page.

See Also

readyState Property

Applies to: IXMLHTTPRequest