↑
Main Page
initError
toString: function () { return this.errorCode.toString() }
};
oXmlDom.__initError__();
oXmlDom.addEventListener(“load”, function () {
this.__initError__();
this.__changeReadyState__(4);
}, false);
return oXmlDom;
} else {
throw new Error(“Your browser doesn’t support an XML DOM object.”);
}
}
Document.prototype.load = function (sURL) {
this.__initError__();
this.__changeReadyState__(1);
this.__load__(sURL);
};
Document.prototype.loadXML = function (sXml) {
this.__initError__();
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.__checkForErrors__();
this.__changeReadyState__(4);
};
Notice that
initError()
is always called before the
readyState
is set to
1
. Likewise,
checkForErrors()
is always called just before
readyState
is set to
4
(which is why it must be called in the
onload
event
handler). These methods must called in order because
onreadystatechange
is called each time the
readyState
changes. If there is old data in the
parseError
object, it must be reset before
onreadystate-
change
is called, otherwise the old data could cause confusion. Along the same lines, the
parseError
object must contain the right data after
readyState
changes to
4
because all processing should be done
by then.
462
Chapter 15
18_579088 ch15.qxd 3/28/05 11:42 AM Page 462
Free JavaScript Editor
Ajax Editor
©
→