Main Page

Detecting Unix operating systems

First step, of course, is to define the variables:
var isMac68K = isMacPPC = false;
Next, check for the various strings in the user-agent string:
if (isMac) {
isMac68K = sUserAgent.indexOf(“Mac_68000”) > -1
|| sUserAgent.indexOf(“68K”) > -1;
isMacPPC = sUserAgent.indexOf(“Mac_PowerPC”) > -1
|| sUserAgent.indexOf(“PPC”) > -1;
With this, you have covered how to determine the Macintosh platform. The only platform left is Unix.
Detecting Unix operating systems
In some ways this is the simplest of the platforms to deal with; in other ways, it is the most difficult. As
you are well aware, Unix comes in many shapes and sizes. There’s SunOS, HP-UX, AIX, Linux, IRIX,
and many more. With each new flavor comes new versioning and different representations in a user-
agent string. In order to avoid being redundant, this section focuses on specifically detecting SunOS and
a few SunOS versions. Using this and the previous information you have been given, you should have
sufficient knowledge to adapt this script to detect any other Unix platforms.
To determine a specific Unix platform, such as SunOS, search the user-agent string for the appropriate
substring. This is actually much easier on Unix, because browsers use the Unix command
uname -sm
include in the user-agent string. Thus, every browser shows the same string for the same operating sys-
tem. Here are some examples for SunOS:
Mozilla/4.0 (compatible; MSIE 6.0; SunOS 5.6 sun4u)
Mozilla/5.0 (X11; U; SunOS 5.6 sun4u; en-US; rv:0.9.4) Gecko/20011128 Netscape6/6.2
Mozilla/4.7 [en] (X11; U; SunOS 5.6 sun4u)
Opera/6.0 (SunOS 5.6 sun4u; U) [en]
Begin by defining a few variables representing the various versions you’re looking for:
var isSunOS = isMinSunOS4 = isMinSunOS5 = isMinSunOS5_5 = false;
For the SunOS, the string to search for is
if (isUnix) {
isSunOS = sUserAgent.indexOf(“SunOS”) > -1;
Next, extract the operating system version by using a regular expression. Because SunOS uses the two-
decimal approach, the expression looks similar to the one used with Mozilla:
var reSunOS = new RegExp(“SunOS (\\d+\\.\\d+(?:\\.\\d+)?)”);
Chapter 8
11_579088 ch08.qxd 3/28/05 11:38 AM Page 248

JavaScript EditorFree JavaScript Editor     Ajax Editor