Main Page


The Browser Detection Script
The browser detection script described in this section uses the user-agent string detection to identify the
following browsers:
Opera 4.0 and higher
Internet Explorer 4.0 and higher
Mozilla 0.9.2 and higher
Safari 1.0 and higher
Netscape Navigator 4.0 – 4.8x
In addition, the methods developed in this chapter fail gracefully and do not cause JavaScript errors in
older browsers that perhaps don’t support ECMAScript Edition 3 fully.
To be practical, it is necessary to detect minimal versions of browsers instead of exact versions. For
instance, this code detects exact versions:
if (isIE5 || isIE6) {
It may not seem like a problem now, but what if IE gets up to version 10? You would be required to keep
adding to this code:
if (isIE5 || isIE6 || isIE7 || isIE8 || isIE9 || isIE10) {
This obviously is not optimal. However, if you test for minimal versions of browsers, the test remains
the same regardless of how many future versions are released:
if (isMinIE5) {
This algorithm never changes, and it represents the way that the browser detection code in this chapter
is developed.
First Steps
The first two steps necessary for browser detection are storing the user-agent string in a local variable
and getting the reported browser version:
var sUserAgent = navigator.userAgent;
var fAppVersion = parseFloat(navigator.appVersion);
Chapter 8
11_579088 ch08.qxd 3/28/05 11:38 AM Page 234

JavaScript EditorFree JavaScript Editor     Ajax Editor