Forget the browser type, just see if the code will work.
Despite
proclamations that the two major browsers - Netscape Navigator and
Microsoft Internet Explorer - are moving towards a common document
object model (DOM), there are still major differences between the DOMs on
these two browsers. There can, indeed, be differences between the DOMs on the
Windows and Mac versions. To add to this, other browsers, such Opera, may use
different DOMs. And older browsers did not use DOMs at all. There is no
guarantee that any new browser will use an established DOM.
February 28, 2001
The
Rules
There is little point in web developers being bloody-minded and
following the rules laid down by the World Wide Web Consortium (W3C), as some
features have not been implemented in any of the common browsers. Take W3C's
specification for HTML. The HTML 4.01 specification includes the tag <q>,
which allows text to be included in quotation marks of the developer's choice.
As at February 2001, neither of the two major browsers, Navigator 6 and Explorer
5.5, recognizes the <q> tag. Check how your browser interprets <q lang="en">Quotation</q>
Here it is as HTML:
Quotation
If there are no quotation marks around the word Quotation,
then your browser has not implemented the <q> tag.
So coding
according to the rules may not work as you might expect it to.
The
Reality
Developers have to accept that the various browsers have
their own individual limitations and that they should code so they can
overcome or work around these limitations. There are two accepted ways of doing
this.
Browser detection. Detecting the browser name, the platform on
which it is supposed to run, and its version number.
Object detection. Detecting whether a specific object is supported
by the browser.
The Case
It has
become accepted by many developers that the "correct" way is to use browser
detection. A developer will code to detect whether the browser is Navigator and
then which version of Navigator. For Navigator 4, it was preferable to use
layers for dynamic content rather than stacks: <layer> can be manipulated
dynamically in Navigator 4, while <div> cannot. If a developer wants to
include stacked dynamic content, they might detect that the browser is Navigator
6 and use <div>, or Navigator 4 and use <layer>, or any older
version of Navigator and use reloaded pages with tables.
It has also
become accepted by many developers that object detection is wrong. This is
because some coders used object detection to identify the browser name and
version. These people produced coding like the following JavaScript.
if (document.layers)
browser="NN4"
else
browser="IE"
The logic is that, as Navigator 4 supported <layers>, checking
for document.layers would identify whether the browser was Navigator 4 or
not.
The problem with this kind of code was that it used object
detection in place of browser detection. Significant problems were raised by
this kind of code when Navigator 6 appeared, as it no longer supported
<layer> and yet did not conform to the IE DOM. It already caused problems
for browsers other than Navigator and Explorer, anyway, especially those which
were based on Mozilla but without support for <layer>, because
<layer> was not in the W3C specification.
A test for the
existence of document.layers only proves that <layer> can be used. It is
object detection, pure and simple.
The following code is common.
if (document.all)
{
[code for Explorer]
}
else
if (document.layers)
{
[code for Navigator]
}
else
{
[code for Other]
}
The code for "Other" is likely to be less sophisticated, often using
tables for formating and deprecated tags for sizes and colours rather than
styles. Using the tests above, Navigator 6 will fall into this "Other"
category.
If a developer does decide to use comprehensive browser
detection, they need the volume of code in the Ultimate JavaScript Browser Sniffer at Netscape's DevEdge site.
This code sets up variables for the browser vendor, version number, JavaScript
version number, and operating system and version. It checks for AOL and WebTV.
It also cannot be installed and forgotten forever. It was updated for Navigator
6 and for Explorer 5.5. Although version 3 of Netscape's sniffer currently
checks for "Navigator 6 and up" and "Explorer 5.5 and up", there is no guarantee
that the "up" will remain valid. A considerable revision of one of these
browsers will require revision of the sniffer.
Time To Re-Think?
I think that we should re-consider whether to reject object
detection. Rather than checking for the browser name and version, so that we can
code <layer> tags if it's Netscape 4, I think that we should consider
checking for the existence of the object. This was done in earlier versions of
HTML. When layers were introduced into Navigator, it was usual to find
<layer>The content of the layer</layer>
<nolayer>The content for older browsers without layers</nolayer>
This was largely abandoned when it became clear that layers were only ever
going to be used in Navigator. The <layer> and <nolayer> tags never
made it into the official HTML specifications. It would therefore be rare to
find people including their <div>s within <nolayer> tags. Instead,
they test for Navigator 4 and only use the <layer> coding if the test is
true.
I suggest that we think about using
if (document.layers)
document.write ("<layer> ... </layer>")
But I suggest this with two conditions.
The test must be relevant. In this case we can test whether layers have
been implemented and use layers if they have. But, based upon the existence of
document.layers, we should not assume that other Navigator 4 objects can be
used. We should not test for one object and then use another. If we want to
use two objects, we should check for each individually.
Usually, the test will not use an "else". If layers have not been
implemented, we should not automatically assume that some other object is
available. We shall need subsequent tests for whatever objects we intend to
use instead.
This means that we will not include tests for
"document.all" to determine if the browser is Explorer 4 and up. We will,
instead, test for the particular object that we intend to use. This means that
we need no longer worry if a new browser appears or if a browser is changed to
use different objects, as happened from Navigator 4 to Navigator 6. The code
will use the appropriate objects if they are available in the browser's
DOM.
We can then continue to improve our web pages by adding new objects
as they appear, while ensuring that old browsers will still be able to interpret
our page content.
David Blakey is a freelance consultant
in information systems and management, specializing in strategic and management
issues. His interest in Web development is primarily from a position of expecting
it to be managed competently and to provide real benefits that increase revenues
or reduce costs. He assists his clients to apply normal business criteria and
practices to Web design and development. Born in Britain, David lives and works
in Auckland, New Zealand. He gives his key skill as pragmatism and his primary
consulting technique as the application of common sense.