How to test for the Javascript DOM? by Riaan Pieterse
Riaan Pieterse
Riaan Pieterse is the CEO and founder of Kerberos Internet Services CC, South Africa. Having spent a number of years conducting various consulting assignments in the Far East, Middle East, Africa and Europe to businesses and governments alike, Riaan has a solid understanding of the business and technology issues in today's market.
For more information visit www.kerberosdev.net or www.kerberosb2b.com
an interesting yet recurring question: "How do I test for the Document
Object Model (DOM) employed by a browser?". Strangely enough I was
asking the same question when starting out in Javascript. However, after
enough
time has passed, with the same thing done more than once, I started to
realise that this is a question that begs answering for once and for
all.
A Typical Test
Testing for the DOM in itself is easy enough. A recommended approach is
testing for the support of a DOM, and not for a browser
version. The following describes Boolean variables that indicates the compliance
to
the DOM methods and parameters that you are targeting:
The above items return a set of true or false values for any browser. This
method still requires that you access objects described by the DOM through
that DOM's methods. In the long run the amount of work you have to do
remains more or less the same.
Javasript is an Object Orientated language
Everyone who is familiar with Javascript knows that the language supports
Object Orientation (OO). Passing objects around in variables is nothing
new, so why do people persist in performing lengthy tests for the DOM
each time we need to access an object?
The item which describes the document's
referencing structure is nothing more that an object itself. This means
that you
only need
to perform the test once, and then proceed to use an arbitrary object
that describes
the DOM object throughout the remainder of your script. However, since
this approach would require that you define a variable for each and every
object you will be referencing, we need an approach which is more
robust.
A Compromise
Typically you access objects though the DOM for one of two reasons: Get
a value, or Set a value. Previous approaches require that you access
the object through the DOM methods each and every time you need to perform
some action on the object. The same holds true for every other object
accessed by your script. What we need is a method that will:
Access the correct DOM using the relevant methods
Return the object of interest
Not waste time and patience
A practical approach used by myself is described in a function that returns
your object without any hassles.
function getDOMObject (documentID){
if (isIE4){
return document.all[documentID];
}else if(isIE6){
return document.getElementById(documentID);
}else if (isNS4){
return document.layers[documentID];
}else if (isNS6){
return document.getElementById(documentID);
}
}
The above function comprimises by using the typical test defined earlier
to identify our browser DOM, and returns the object identified by its
ID / NAME pair. So whenever you need to do something to an object, this
approach requires that you call the getDOMObeject () function. For example,
the following will set the value attribute of a hypothetical text box
to 'test value'.
In the article Dementia: Just What Is It, we have learned about a frightening term, Dementia, and just what it is or, rather, how it manifests itself in the human condition. I gave 5 examples from my personal knowledge, including myself.
There is no data as yet that indicates how many former patients of Pfizer's anti-inflammatory and painkilling drug are filing Celebrex law suits, but given the magnitude of the company's perceived crime it is likely that there will be very many. And even a quick perusal of the alleged behaviour of the company regarding this drug seems to point to Celebrex law suits being something of a fait accompli.
Eckhart Tolle lived upto his twenty ninth year in a state of almost continual anxiety interspersed with periods of suicidal depression. Then he woke up one night with a feeling of absolute dread. The silence of the night, the vague outlines of the furniture in the dark room, the distant noise of a passing train - everything felt so alien, so hostile, and so utterly meaningless that it created in him a deep loathing of the world. "I cannot live with myself any longer." This was the thought that kept repeating itself in his mind. Suddenly he became aware that if he could not live with himself, there had to be two - he and the "self" he could not live with. He was stunned by the realization. He became enveloped by powerful feelings.