| home / programming / javascript / domwrapper | [next] |
|
By Nicholas C. Zakas (nicholas@nczonline.net)
In my opinion, one of the most useful parts of JavaScript is the DOM Document object. With this object, a developer not only can build a DOM Document from scratch, but also can load a file via HTTP into it. The result is the possibility of having client-side JavaScript being loaded with server-side information. What could possibly be better? That answer is simple: if there was a common interface for the DOM Document between Internet Explorer and Netscape.
This article will focus on creating a single DOM Document interface for developers to use in both Internet Explorer 5.0+ (Windows) and Mozilla (The Mozilla browser itself, or Netscape 6.1+). This interface will use both IE's ActiveX approach as well as Mozilla's standards-compliant approach. Because the IE implementation uses ActiveX technology, this will not work on IE for the Mac.
Developers have access to a full library of XML-related objects through Microsoft's MSXML Parser. Each of these objects is an ActiveX object. You can create a DOM Document in Internet Explorer by doing the following:
var objXMLDOM = new ActiveXObject("Msxml.DOMDocument");
Mozilla follows the W3C's DOM standard (http://www.w3.org/DOM/). As such, you create a DOM Document in the following way:
var objXMLDOM =
document.implementation.createDocument("", "", null);
The interesting thing to note is that because an ActiveX
object is compiled code on the client machine (Note: not the client Web
browser), you cannot have expando properties. The
following will cause an error in IE, but not in Mozilla:
objXMLDOM.myAttribute = "blue";
What this means for us is that if we want to extend one interface to be more compatible with the other, it will have to be the Mozilla interface that is customized to be more compatible with the IE interface.
As part of this process, we are going to make a JavaScript
factory. A factory is a type of object that serves only as a method to access a
group of functions. For instance, the following uses the native JavaScript Date
object as a factory:
var bIsValidDate = Date.parse(strDate);
Note that we didn't instantiate a Date
object; we just used one of its methods. We will create a factory called jsXML,
which will have one method called createDOMDocument().
Let's set up the JavaScript file:
//define factory
function jsXML() { }
//define method
jsXML.createDOMDocument = function() {}
| home / programming / javascript / domwrapper | [next] |
Created: June 13, 2002
Revised: June 13, 2002
URL: http://webreference.com/programming/javascript/domwrapper/