Cookies improve the way our website works, by using this website you are agreeing to our use of cookies. For more information see our privacy policy.
OK
The event object is accessible to all event handlers in all browsers.
The properties of the event object contain additional information about the current event.
To get further details about these properties and the possible event handler registration methods, please see the page for the event object.
The onreadystatechange event occurs when the load state of the XMLDocument object changes.
The onload event occurs when the loading process is completed.
Example HTML code 1:
This example illustrates the use of the ondataavailable event:
Code
ajax.js
chars.xml
<head><scripttype="text/javascript"src="ajax.js"></script><scripttype="text/javascript">var xmlDoc = null;
function LoadXML () {
xmlDoc = CreateXMLDocumentObject (); // defined in ajax.js
if (!xmlDoc) {
return;
}
var url = "chars.xml";
xmlDoc.async = true;
if ('ondataavailable'in xmlDoc) { // Internet Explorer
xmlDoc.ondataavailable = OnData;
}
if (xmlDoc.addEventListener) { // all browsers except IE
xmlDoc.addEventListener("load", OnLoadXML, false);
}
else { // IE
xmlDoc.onreadystatechange = OnStateChange;
}
xmlDoc.load (url);
}
function OnData () {
// The currently available data as a text
var dataTxt = xmlDoc.documentElement.xml;
dataTxt = dataTxt.replace (/</g, "<");
var output = document.getElementById ("output");
output.innerHTML += "A part of the data has arrived. The currently available data:<div style='color:#808080'>" + dataTxt + "</div>";
}
function OnLoadXML () {
LoadingFinished ();
}
function OnStateChange () {
var output = document.getElementById ("output");
switch (xmlDoc.readyState) {
case1:
output.innerHTML += "The load method was called, but no data has been received yet.<br/>";
break;
case2:
output.innerHTML += "A part of the data has been loaded, but the document object model (DOM) is not yet available.<br/>";
break;
case3:
output.innerHTML += "The document object model has been built from the received data and it is available.<br/>";
break;
case4:
LoadingFinished ();
break;
};
}
function LoadingFinished () {
var output = document.getElementById ("output");
output.innerHTML += "All data has been loaded, the operation is finished:<br/>";
var errorMsg = null;
if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
errorMsg = "XML Parsing Error: <span style='color:red'>" + xmlDoc.parseError.reason
+ " at line " + xmlDoc.parseError.line
+ " at position " + xmlDoc.parseError.linepos
+ "</span><br/>";
}
else {
if (xmlDoc.documentElement) {
if (xmlDoc.documentElement.nodeName == "parsererror") {
errorMsg = "<span style='color:red'>" + xmlDoc.documentElement.childNodes[0].nodeValue + "</span><br/>";
}
}
}
if (errorMsg) {
output.innerHTML += errorMsg;
}
else {
output.innerHTML += "<span style='color:blue'>The XML is valid.</span><br/>";
}
}
</script></head><body><buttononclick="LoadXML ()">Analyze the loading process of an XML</button><br/><br/><divid="output"style="width:520px; height:200px; overflow:auto; border:1px solid #000000; background-color:#f5e4b1;"></div></body>
function CreateHTTPRequestObject () {
// although IE supports the XMLHttpRequest object, but it does not work on local files.
var forceActiveX = (window.ActiveXObject && location.protocol === "file:");
if (window.XMLHttpRequest && !forceActiveX) {
returnnewXMLHttpRequest();
}
else {
try {
returnnewActiveXObject("Microsoft.XMLHTTP");
} catch(e) {}
}
alert ("Your browser doesn't support XML handling!");
returnnull;
}
function CreateMSXMLDocumentObject () {
if (typeof (ActiveXObject) != "undefined") {
var progIDs = [
"Msxml2.DOMDocument.6.0",
"Msxml2.DOMDocument.5.0",
"Msxml2.DOMDocument.4.0",
"Msxml2.DOMDocument.3.0",
"MSXML2.DOMDocument",
"MSXML.DOMDocument"
];
for (var i = 0; i < progIDs.length; i++) {
try {
returnnewActiveXObject(progIDs[i]);
} catch(e) {};
}
}
returnnull;
}
function CreateXMLDocumentObject (rootName) {
if (!rootName) {
rootName = "";
}
var xmlDoc = CreateMSXMLDocumentObject ();
if (xmlDoc) {
if (rootName) {
var rootNode = xmlDoc.createElement (rootName);
xmlDoc.appendChild (rootNode);
}
}
else {
if (document.implementation.createDocument) {
xmlDoc = document.implementation.createDocument ("", rootName, null);
}
}
return xmlDoc;
}
function ParseHTTPResponse (httpRequest) {
var xmlDoc = httpRequest.responseXML;
// if responseXML is not valid, try to create the XML document from the responseText property
if (!xmlDoc || !xmlDoc.documentElement) {
if (window.DOMParser) {
var parser = newDOMParser();
try {
xmlDoc = parser.parseFromString (httpRequest.responseText, "text/xml");
} catch (e) {
alert ("XML parsing error");
returnnull;
};
}
else {
xmlDoc = CreateMSXMLDocumentObject ();
if (!xmlDoc) {
returnnull;
}
xmlDoc.loadXML (httpRequest.responseText);
}
}
// if there was an error while parsing the XML document
var errorMsg = null;
if (xmlDoc.parseError && xmlDoc.parseError.errorCode != 0) {
errorMsg = "XML Parsing Error: " + xmlDoc.parseError.reason
+ " at line " + xmlDoc.parseError.line
+ " at position " + xmlDoc.parseError.linepos;
}
else {
if (xmlDoc.documentElement) {
if (xmlDoc.documentElement.nodeName == "parsererror") {
errorMsg = xmlDoc.documentElement.childNodes[0].nodeValue;
}
}
}
if (errorMsg) {
alert (errorMsg);
returnnull;
}
// ok, the XML document is valid
return xmlDoc;
}
// returns whether the HTTP request was successful
function IsRequestSuccessful (httpRequest) {
// IE: sometimes 1223 instead of 204
var success = (httpRequest.status == 0 ||
(httpRequest.status >= 200 && httpRequest.status < 300) ||
httpRequest.status == 304 || httpRequest.status == 1223);
return success;
}