spacer

Webref WebRef   Sitemap · Experts · Tools · Services · Newsletters · About i.com

home / programming / javascript / domwrapper current pageTo page 2To page 3To page 4To page 5
[next]

A Cross-Browser DOM Document Wrapper

Developer News
Mandrake Linux Founder Back, Virtually
Amazon: We're a Technology Company
Sun Expands MySQL With Closed Source

By Nicholas C. Zakas (nicholas@nczonline.net)

Introduction

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.

A Little Background

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.

The Factory

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 current pageTo page 2To page 3To page 4To page 5
[next]

internet.comearthweb.comDevx.commediabistro.comGraphics.com

Search:

Jupitermedia Corporation has two divisions: Jupiterimages and JupiterOnlineMedia

Jupitermedia Corporate Info

Legal Notices, Licensing, Reprints, Permissions, Privacy Policy.
Advertise | Newsletters | Tech Jobs | Shopping | E-mail Offers

webref The latest from WebReference.com Browse >
Working with the DOM Stylesheets Collection · Administering RBAC in PHP 5 CMS Framework · xref: Automatic Cross Referencing Script
Sitemap · Experts · Tools · Services · Email a Colleague · Contact FREE Newsletters 
 The latest from internet.com
Combine BottomCount() with Other MDX Functions to Add Sophistication · Creating a Daemon with Python · The Coming Voice-over-WiMAX Revolution

Created: June 13, 2002
Revised: June 13, 2002

URL: http://webreference.com/programming/javascript/domwrapper/