|MySQL Conference and Expo April 14-17, 2008, Santa Clara, CA|
Object Detectionby Apple Developer Connection
and Danny Goodman, author of Dynamic HTML: The Definitive Reference
The pace of new browser releases may be
slower than it was in the early days, but developers must still
confront a bemusing array of browser versions and brands that
problem, scripters commonly provide two or more code branches so
that a browser follows an execution path containing statements that
it supports. Browser sniffing -- the task of inspecting
Browser Sniffing Woes
The fundamental belief behind branching code for a particular browser brand and/or version (and possibly operating system) is that Version N of Browser Brand X implements a known set of core scripting language and object model features. Scripters frequently extend this belief to mean that Versions N and later of the browser support a particular feature. Unfortunately, these beliefs have several flaws.
Let's start by looking at the browser
version number. The
Next, you have the "equals" and "greater than or equals" version conundrum. When scripters started sniffing browsers in earnest for Netscape and IE brands of Version 4 browsers, some scripts simply checked whether the version number (regardless of how it was extracted) equaled 4. When IE5 appeared, the scripts failed to use the IE4 powers because 5 does not equal 4. Conversely, those who thought they would be smart and write their Netscape 4 sniffers to accept versions 4 or later got into trouble when NN6 appeared and their NN4 layer-dependent code broke left and right.
On the one hand, the "equals 4" camp failed to think beyond solving a problem for the current browser. On the other hand, the "greater than or equals 4" group got caught in a rare, but obviously not impossible, case of an object model feature (the layer object) being yanked by the browser's maker in its drive to adopt the W3C DOM standard. While it's easy to forecast that there will be a succeeding version of a browser, it's not easy to predict feature deprecation or removal in future browsers. It should be clear, in retrospect, that both browser sniffing approaches required emergency code repair as new browser versions starting knocking on site doors.
Lastly, what most browser sniffers tend to
ignore are scriptable browsers other than IE and NN. Opera is
perhaps the most popular of the "other" category, but more are out
there. We also need to prepare for future browsers that are built
from the core code that goes into IE and NN (the latter from the
mozilla.org group) but that won't necessarily identify themselves by
the familiar brands we've come to know and love. If Frobnitz Corp.
should produce the Frobigator 1.0 browser from the latest
mozilla.org engine, the browser could offer extraordinary W3C DOM
scripting facilities; but a typical browser sniffer would relegate
it to the "dumb and dumber" category, because string parsing of
Simple Object Detection
You can solve many of these problems with a technique called object detection. This is a very broad name for an approach that allows scripts to create execution branches based on whether the current browser supports a desired object, property, or method.
You have probably seen object detection at
work already without recognizing how powerful it can be. Netscape
Navigator 2 (all OS versions) and IE3/Windows did not support
scripting for the image rollover effect because IMG elements were
not recognized as objects in the DOM. But subsequent browsers
implemented the image object, exposing all IMG elements within a
page as an array of image objects belonging to the
This syntax works because in non-supporting
browsers, the expression
Pay close attention to the elegance of this admittedly simple example. Mouse-related event handlers operate on all scriptable browsers, but only those browsers that manipulate images as objects attempt to act on those objects. This is an excellent example of using scripting to add value to a page for those browsers that support specific functionality.
Detecting Supported Properties
With today's more complex
object models, it's not uncommon to branch code based on support for
an object's property and/or method. For example, in IE4 and later,
Problems (script errors) occur, however,
The reason this works is that when an
Detecting Supported Methods
Functions within a page expose themselves as objects
Since object methods (the method name
without trailing parentheses) return a value when evaluated, you can
easily test for a browser's support of a method. You can reference a
method in a conditional expression, just as if it were a property of
an object. For example, to see if the browser supports the
The only glitches with verifying methods occurs with several earlier browsers, notably NN2, IE3/Windows, and IE/Mac prior to version 5. The problems occur when the method is, in truth, supported by the browser: Testing for the presence of an existing method in conditional expressions causes a script error in IE and a misleading evaluation in NN2. The lowest common denominator of browsers that permit method detection are those that support the W3C DOM in some fashion. In other words, for maximum compatibility, you should perform method testing for methods that belong to the W3C DOM. Because support for various W3C DOM objects and methods varies so widely among browser versions, this testing can help scripts gracefully weave their way through potential minefields.
One Gotcha and Its Workaround
Before you use property
checking, you need to be aware of the data types and default values
of the properties you're checking. If the value of a supported
property is zero or an empty string, a reference to the property in
As a kind of lazy alternative, you can test
for the presence of a property by seeing if its type is anything
You're not completely off the hook with this operator, however. It is missing from the core language in Netscape Navigator 2. Therefore, you should use this technique only within execution branches that have passed tests indicating the browser is something other than NN2 -- or assume that no one using that admittedly ancient browser will venture to your site.