↑
Main Page
Full Script
After the version is extracted, you must use the
compareVersions()
function (explained in a previous
section) to determine the minimum operating system versions:
if (isUnix) {
isSunOS = sUserAgent.indexOf(“SunOS”) > -1;
if (isSunOS) {
var reSunOS = new RegExp(“SunOS (\\d+\\.\\d+(?:\\.\\d+)?)”);
reSunOS.test(sUserAgent);
isMinSunOS4 = compareVersions(RegExp[“$1”], “4.0”) >= 0;
isMinSunOS5 = compareVersions(RegExp[“$1”], “5.0”) >= 0;
isMinSunOS5_5 = compareVersions(RegExp[“$1”], “5.5”) >= 0;
}
}
With that, you can now use
isSunOS
,
isMinSunOS4
,
isMinSunOS5
, and
isMinSunOS5_5
.
Of course, all this knowledge of browsers and operating systems is useless unless you can come up with
a practical way to use it.
The Full Script
The entire script is listed here for your convenience. Note that the order in which the various checks
appear is very important. The complete script should be stored in a JavaScript such as detect.js.
var sUserAgent = navigator.userAgent;
var fAppVersion = parseFloat(navigator.appVersion);
function compareVersions(sVersion1, sVersion2) {
var aVersion1 = sVersion1.split(“.”);
var aVersion2 = sVersion2.split(“.”);
if (aVersion1.length > aVersion2.length) {
for (var i=0; i < aVersion1.length - aVersion2.length; i++) {
aVersion2.push(“0”);
}
} else if (aVersion1.length < aVersion2.length) {
for (var i=0; i < aVersion2.length - aVersion1.length; i++) {
aVersion1.push(“0”);
}
}
for (var i=0; i < aVersion1.length; i++) {
if (aVersion1[i] < aVersion2[i]) {
return -1;
} else if (aVersion1[i] > aVersion2[i]) {
return 1;
}
249
Browser and Operating System Detection
11_579088 ch08.qxd 3/28/05 11:38 AM Page 249
Free JavaScript Editor
Ajax Editor
©
→