Represents an XML request usingHTTP.
Members Table
The following table lists the members exposed by the
XMLHttpRequest
object.
Remarks
The XMLHttpRequest property is available on the window object in Internet Explorer 7.
var oReq = new XMLHttpRequest;
With the XMLHttpRequest object, Internet Explorer clients can retrieve and submit XML data directly to a Web server without reloading the page. To convert XML data into renderable HTML content, use the client-side XML DOM or Extensible Stylesheet Language Transformations (XSLT) to compose HTML elements for presentation.
The native scripting object also supports the use of expandos (custom properties), and properly recognizes the 'this' notation of JavaScript.
To support versions of Internet Explorer prior to Internet Explorer 7, use the following function to get the XMLHttpRequest object:
function getXMLHttpRequest()
{
if (window.XMLHttpRequest) {
return new window.XMLHttpRequest;
}
else {
try {
return new ActiveXObject("MSXML2.XMLHTTP.3.0");
}
catch(ex) {
return null;
}
}
}
Examples
The following script demonstrates how to create and use the XMLHttpRequest object. For best client-side performance, the XMLHTTP request is asynchronous and uses anonreadystatechange event handler to process the data returned by the call. The script uses the getXMLHttpRequest() function defined above to create the request object.
function handler()
{
if (oReq.readyState == 4 /* complete */) {
if (oReq.status == 200) {
alert(oReq.responseText);
}
}
}
var oReq = getXMLHttpRequest();
if (oReq != null) {
oReq.open("GET", "http://localhost/test.xml", true);
oReq.onreadystatechange = handler;
oReq.send();
}
else {
window.alert("AJAX (XMLHTTP) not supported.");
}
The following example demonstrates the same functionality in Microsoft Visual Basic Scripting Edition (VBScript).
<script type="text/vbscript">
Dim objXML
Function objXML_onreadystatechange()
If (objXML.readyState = 4) Then
If (objXML.status = 200) Then
MsgBox objXML.responseText, 0, objXML.statusText
End If
End If
End Function
Function Window_onload()
If IsObject(window.XmlHttpRequest) Then
objXML = window.XmlHttpRequest
Else
Set objXML = CreateObject("MSXML2.XMLHTTP.3.0")
End If
objXML.Open "GET", "http://localhost/test.xml", True
objXML.OnReadyStateChange = GetRef("objXML_onreadystatechange")
objXML.Send
End Function
</script>
In Internet Explorer 8, the VBScript syntax is not supported, which complicates native support detection somewhat. One way to detect Internet Explorer 8 is by using conditional comments. The following comment detects Internet Explorer 8 when it is not running in compatibility mode. The script sets a global variable that can be checked later when creating the XMLHttpRequest object.
<!--[if IE 8]><script type="text/vbscript">
vbIE8 = True
</script><![endif]-->
If Not vbIE8 And IsObject(window.XmlHttpRequest) Then ...
Standards Information
This object is defined in
The XMLHttpRequest Object (W3C Working Draft)
.