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.

Chapter 15<br />

for (var i=0; i < arrSignatures.length; i++) {<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 />

oXmlDom.parseError = {<br />

valueOf: function () { return this.errorCode; },<br />

toString: function () { return this.errorCode.toString() }<br />

};<br />

oXmlDom.__initError__();<br />

oXmlDom.addEventListener(“load”, function () {<br />

this.__changeReadyState__(4);<br />

}, false);<br />

return oXmlDom;<br />

}<br />

} else {<br />

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

}<br />

This code uses object literal notation to create the parseError object in order to save space. The valueOf()<br />

method is defined to return the errorCode property, which is the same as IE’s implementation; the<br />

toString() method also returns the errorCode property, but as a string primitive. The initError()<br />

method initializes all the parseError object’s properties. Here’s the code:<br />

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

this.parseError.errorCode = 0;<br />

this.parseError.filepos = -1;<br />

this.parseError.line = -1;<br />

this.parseError.linepos = -1;<br />

this.parseError.reason = null;<br />

this.parseError.srcText = null;<br />

this.parseError.url = null;<br />

};<br />

The next step is to check for a parsing error. This must be done in both the load() and loadXML() methods<br />

because a parsing error can occur in either one. Because it’s bad coding practice to have the same code<br />

in two places, it’s best to create a new method to handle parsing errors:<br />

460

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

Saved successfully!

Ooh no, something went wrong!