18.04.2016 Views

Professional JavaScript For Web Developers

javascript for learners.

javascript for learners.

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

XML in <strong>JavaScript</strong><br />

try {<br />

var oXmlDom = new ActiveXObject(arrSignatures[i]);<br />

return oXmlDom;<br />

}<br />

} catch (oError) {<br />

//ignore<br />

}<br />

throw new Error(“MSXML is not installed on your system.”);<br />

} else if (document.implementation && document.implementation.createDocument) {<br />

var oXmlDom = document.implementation.createDocument(“”,””,null);<br />

return oXmlDom;<br />

}<br />

} else {<br />

throw new Error(“Your browser doesn’t support an XML DOM object.”);<br />

}<br />

The next task is to make Mozilla support the readyState property and the onreadystatechange<br />

event handler. This requires you to make some additional changes to the Document class.<br />

First, add a readyState property and initialize it to 0.<br />

Document.prototype.readyState = 0;<br />

Next, create an onreadystatechange property and assign it the value of null:<br />

Document.prototype.onreadystatechange = null;<br />

Whenever the readyState property changes, the onreadystatechange function must be called. To<br />

facilitate this, it’s best to create a method:<br />

Document.prototype.__changeReadyState__ = function (iReadyState) {<br />

this.readyState = iReadyState;<br />

};<br />

if (typeof this.onreadystatechange == “function”) {<br />

this.onreadystatechange();<br />

}<br />

This method takes the new ready state as an argument and assigns it to the readyState property. Be<br />

sure you check that onreadystatechange is actually a function before calling it (otherwise, this causes<br />

an error). Because this method shouldn’t be called outside of the Document object, it uses the <strong>JavaScript</strong><br />

notation for a private method (leading and trailing double underscores).<br />

Internet Explorer’s XML DOM supports five ready states, but there is no way to mimic all of them for<br />

Mozilla. Really, the only important value for readyState is 4, which indicates that the XML DOM is<br />

457

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!