Main Page

IE branch

This code uses object/feature detection to determine which way to go. Because Internet Explorer on
Windows is the only browser that supports the
ActiveXObject
class, that is a fair way to test for IE. The
second test is a generic one determining if the browser supports the DOM standard
createDocument()
method. Even though Mozilla is the only browser that currently supports this, it’s conceivable that other
browsers may adopt this functionality in the future, so testing in this way makes the code future-proof. If
neither statement evaluates to
true
, then the constructor throws an error indicating no XML DOM object
is available.
The IE branch
For the IE section of the constructor, just insert the code from the
createXMLDOM()
function earlier in
the chapter:
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 {
var oXmlDom = new ActiveXObject(arrSignatures[i]);
return oXmlDom;
} catch (oError) {
//ignore
}
}
throw new Error(“MSXML is not installed on your system.”);
} else if (document.implementation && document.implementation.createDocument) {
//DOM-specific code
} else {
throw new Error(“Your browser doesn’t support an XML DOM object.”);
}
}
That’s all you do for this to work in IE. The more complicated part has to do with Mozilla.
The Mozilla branch
The first step in the Mozilla branch is to create the XML DOM object using
createDocument()
:
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++) {
456
Chapter 15
18_579088 ch15.qxd 3/28/05 11:42 AM Page 456


JavaScript EditorFree JavaScript Editor     Ajax Editor


©