↑
Main Page
loadXML
completely loaded and ready for use. This value is easy to mimic by assigning an
onload
event handler.
It’s also easy to simulate
readyState 1
, which indicates that the XML DOM has just started loading.
The other ready states aren’t as important and even more difficult — if not impossible — to simulate.
The two methods that affect the
readyState
property are
loadXML()
and
load()
. The
loadXML()
method is easy to update because it’s your creation. Just add two lines of code:
Document.prototype.loadXML = function (sXml) {
this.__changeReadyState__(1);
var oParser = new DOMParser();
var oXmlDom = oParser.parseFromString(sXml, “text/xml”);
while (this.firstChild) {
this.removeChild(this.firstChild);
}
for (var i=0; i < oXmlDom.childNodes.length; i++) {
var oNewNode = this.importNode(oXmlDom.childNodes[i], true);
this.appendChild(oNewNode);
}
this.__changeReadyState__(4);
};
The updated
loadXML()
method sets the
readyState
to
1
at the beginning and
4
at the end.
To update the
load()
method, start by creating a pointer to the original load method:
Document.prototype.__load__ = Document.prototype.load;
Next, define a new
load()
method that sets the
readyState
property to
1
and then calls the original
load()
method:
Document.prototype.load = function (sURL) {
this.__changeReadyState__(1);
this.__load__(sURL);
};
In order to set the
readyState
property to
4
at the right time, use the
onload
event handler. Because
you can only assign an event handler after the XML DOM object has been instantiated, this must take
place back in the
XmlDom
constructor:
function XmlDom() {
if (window.ActiveXObject) {
var arrSignatures = [“MSXML2.DOMDocument.5.0”, “MSXML2.DOMDocument.4.0”,
“MSXML2.DOMDocument.3.0”, “MSXML2.DOMDocument”,
“Microsoft.XmlDom”];
for (var i=0; i < arrSignatures.length; i++) {
try {
458
Chapter 15
18_579088 ch15.qxd 3/28/05 11:42 AM Page 458
Free JavaScript Editor
Ajax Editor
©
→