| home / programming / javascript / domwrapper | [previous] [next] |
|
createDOMDocument() FunctionThe first step to creating a cross-browser interface is to
have a common way to create the DOM Document. The createDOMDocument()
method will serve this purpose. The
first step is to determine which browser is being used and then create the
DOM Document in the appropriate way. However, IE throws a little wrinkle at us.
The DOM Document in IE is an ActiveX object, as discussed before. What this means for us is that even though a user has IE 5.0 on their computer, they may not necessarily have the latest version of MSXML. Of course, we want to use the best and most current version, but how do we know what the user's machine currently has installed?
Unfortunately, there is no pretty way to determine what
version of MSXML the user has on their machine. The only way to determine if an
ActiveX object can be created is to try to create it. If the ActiveX object is
not available on the client machine, it will cause a JavaScript error. So in
order to find out what version of MSXML the user has installed, we have to try
to create each ActiveX object and look for one that doesn't cause an error
using a try...catch block.
First, we'll define the array of possible ActiveX objects to use. This array is in the order of most recent version to least recent, which will allow us to cycle through the array in order to make sure we get the most current version available on the user's machine:
var ARR_ACTIVEX = ["MSXML4.DOMDocument",
"MSXML3.DOMDocument",
"MSXML2.DOMDocument",
"MSXML.DOMDocument",
"Microsoft.XmlDom"]
Next, we define a constant string that will be filled with the appropriate prefix when it is determined:
var STR_ACTIVEX = "";
Now, here comes the try...catch block:
//if this is IE, determine which string to use
if (isIE) {
//define found flag
var bFound = false;
//iterate through strings to determine which one to use
for (var i=0; i < ARR_ACTIVEX.length && !bFound; i++) {
//set up try...catch block for trial and error
//of strings
try {
//try to create the object, it will cause an
//error if it doesn't work
var objXML = new ActiveXObject(ARR_ACTIVEX[i]);
//if it gets to this point, the string worked,
//so save it
STR_ACTIVEX = ARR_ACTIVEX[i];
bFound = true
} catch (objException) {
} //End: try
} //End: for
//if we didn't find the string, send an error
if (!bFound)
throw "MSXML not found on your computer."
}
With the ActiveX string now determined, we can continue on
to create the createDOMDocument() method with a
simple browser test:
jsXML.createDOMDocument = function() {
//variable for the created DOM Document
var objDOM = null;
//determine if this is a standards-compliant browser like Mozilla
if (document.implementation &&
document.implementation.createDocument) {
//create the DOM Document the standards way
objDOM = document.implementation.createDocument("","", null);
} else if (isIE) {
//create the DOM Document the IE way
objDOM = new ActiveXObject(STR_ACTIVEX);
}
//return the object
return objDOM;
}
The next step in this process is to determine the parameters for this method. In IE's interface, there are no parameters to the creation of the DOM Document. In Mozilla's interface, there are three parameters. The first two are the namespace and tag name for the root node (document element) of the DOM Document that is being created. The third is an object representing the document type that is being created. The third parameter has not yet been activated in Mozilla (according to their documentation), so only the first two are of interest.
If we were to create a DOM Document in Mozilla like this:
var objDOM = document.implementation.createDocument("http://www.nczonline.net/", "myroot", null);
The resulting XML string would be:
<a0:myroot xmlns:a0="http://www.nczonline.net/" />
That seems to be a pretty useful thing, to be able to
initialize the DOM Document with a namespace and root tag name, so let's make
two parameters for our createDOMDocument()
method: the namespace and root tag name. The important thing to note is that if
there is only a namespace and no root tag name specified, it has no effect. If,
however, there is a root tag name specified and no namespace, it still works to
produce an XML string.
So first, we add the parameters into the function definition:
jsXML.createDOMDocument = function(strNamespaceURI, strRootTagName) {
...
}
For Mozilla, these two parameters can be passed through to the native creation method:
//create the DOM Document the standards way
objDOM = document.implementation.createDocument(strNamespaceURI, strRootTagName, null);
In IE, we will have to do the work ourselves. We can check
for the parameters and use IE's proprietary loadXML()
method to simulate Mozilla's implementation:
//create the DOM Document the IE way
objDOM = new ActiveXObject(STR_ACTIVEX);
//if there is a root tag name, we need to preload the DOM
if (strRootTagName) {
//If there is both a namespace and root tag name, then
//create an artifical namespace reference and load the XML.
if (strNamespaceURI) {
objDOM.loadXML("<a0:" + strRootTagName + "xmlns:a0=\"" +
strNamespaceURI + "\" />");
} else {
objDOM.loadXML("<" + strRootTagName + "/>");
}
}
| home / programming / javascript / domwrapper | [previous] [next] |
| ||||||||||||||||||||
Created: June 13, 2002
Revised: June 13, 2002
URL: http://webreference.com/programming/javascript/domwrapper/2.html