|
Help
CodingForums Partners
This is a
![]() |
|
| Return values for the readyState property | Description |
| 0 | The object has not yet started initializing (uninitialized). |
| 1 | The object is initializing, but no data is being read (loading). |
| 2 | Data is being loaded into the object and parsed (loaded). |
| 3 | Parts of the object's data has been read and parsed, so the object model is available. However, the complete object data is not yet ready (interactive). |
| 4 | The object has been loaded, its content parsed (completed). |
The value we're interested in here is the last one- "4" indicating the object has fully loaded. Using it, the below code will return true if our XML file has completely loaded:
if (xmlDoc.readyState==4)
alert("XML file loaded!")
- Creating an intelligent XML file download detector (IE only)
With the readyState property and a little free time, it's not hard to create a mechanism for detecting when an XML object has loaded, and abort the rest of the script should this process take longer than the designated time frame (ie: 5 seconds). This keeps the unwelcome surprises at bay as far as dealing with slow or non loading XML files. Take a look at the below code:
var xmlDoc = new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.load("myfile.xml");
var times_up=false
var expiretime=5000 //set expire time to abort. 5 seconds.
function XMLloadcontrol(){
if (times_up==true){ //if designated time frame has expired
alert ("XML file loading aborted")
return
}
else if (xmlDoc.readyState==4){ //if xml file has loaded within time frame
getdaily() //execute the rest of the script, ie: function getdaily()
return
}
else //else, run thyself again
setTimeout("test()",10)
}
if (window.ActiveXObject){ //if IE
setTimeout("times_up=true", expiretime) //set abort download time (5 sec).
XMLloadcontrol()
}
All that our routine really does is continuously check the readyState of the XML file up until 5 seconds has elapsed, and performs two different actions depending on the outcome. Here we assume getdaily() is the function that is to be run should the XML file downloads successfully.
Ok, with the potential hazards of your eager script too quickly accessing the XML file avoided, we can now move on to see how to actually manipulate the file.