Main Page

Detecting Internet Explorer

var reAppleWebKit = new RegExp(“AppleWebKit\\/(\\d+(?:\\.\\d*)?)”);
reAppleWebKit.test(sUserAgent);
var fAppleWebKitVersion = parseFloat(RegExp[“$1”]);
isMinSafari1 = fAppleWebKitVersion >= 85;
isMinSafari1_2 = fAppleWebKitVersion >= 124;
} else if (isKonq) {
var reKonq = new RegExp(“Konqueror\\/(\\d+(?:\\.\\d+(?:\\.\\d)?)?)”);
reKonq.test(sUserAgent);
isMinKonq2_2 = compareVersions(RegExp[“$1”], “2.2”) >= 0;
isMinKonq3 = compareVersions(RegExp[“$1”], “3.0”) >= 0;
isMinKonq3_1 = compareVersions(RegExp[“$1”], “3.1”) >= 0;
isMinKonq3_2 = compareVersions(RegExp[“$1”], “3.2”) >= 0;
}
}
In this section of the code, check whether the
compareVersions()
returns a value greater-than or equal
to zero, which indicates that the versions are either equal (if it returns 0) or that the first version is
greater than the second (if it returns 1).
The detection for KHTML-based browsers is complete. You can either just use
isKHTML
if you don’t care
which browser is being used, or use the more specific variables to determine the browser and version.
Detecting Internet Explorer
As discussed earlier, the IE user-agent string is quite unique. Recall the user-agent string for IE 6.0:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT)
When you compare this to other browsers, two parts stand out as unique:
“compatible”
and
“MSIE”
.
This is the basis for detecting IE:
var isIE = sUserAgent.indexOf(“compatible”) > -1
&& sUserAgent.indexOf(“MSIE”) > -1;
This seems to be straightforward, but there is a problem. Take a second look at the Opera user-agent
string when it is disguised as IE 6.0:
Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1) Opera 7.54
See the problem? If you check for only
“compatible”
and
“MSIE”
, then Opera disguised as IE also returns
true
. The solution is to use the
isOpera
variable (explained previously) to ensure proper detection:
var isIE = sUserAgent.indexOf(“compatible”) > -1
&& sUserAgent.indexOf(“MSIE”) > -1
&& !isOpera;
Next, define variables for the different IE versions:
var isMinIE4 = isMinIE5 = isMinIE5_5 = isMinIE6 = false;
241
Browser and Operating System Detection
11_579088 ch08.qxd 3/28/05 11:38 AM Page 241


JavaScript EditorFree JavaScript Editor     Ajax Editor


©