The Platform/Operating System Detection Script - Java Script

Now that you have delved into the world of browser detection at great length,you must meet another challenge:figuring out the operating system on the client’s machine.Even though the browser companiessay that their browsers act the same across different platforms and operating systems,it is not so.

Take the case of Internet Explorer.On Windows,a powerful interface allows Microsoft ActiveX controlsto be embedded in pages or used in JavaScript.The problem is that these ActiveX controls requireWindows to work.So even though Microsoft says that IE on Unix and Macintosh works the same as IEon Windows, you know that this is impossible.Therefore, you must,at least, be able to tell which operatingsystem you are dealing with in order to determine if special accommodations must be made.

Methodology

The method for determining the operating system is to start by looking for the platform.For the purposesof this book,the platforms are divided into three groups: Windows, Macintosh, and Unix.

After the platform is determined,it is then possible to determine some operating system information.For Windows or Unix,you can actually pull out the operating system version.For Macintosh, however,you cannot.the Macintosh platform provides information only if the processor is a 68000 chip or aPowerPC, although Safari includes the string “MacOS X” (but then again, Safari runs only on MacOS X,so is that really helpful?).

Typically,determining the platform alone is good enough for making appropriate JavaScript branches.However, sometimes additional operating system information is important,and this script providesfor that.

First steps

So how does one go about determining the platform of the client user? Once again,the navigator object comes to the rescue with its platform property.But as usual, things aren’t as easy as they seem.Indeed,each browser provides different information to navigator. platform.For instance, IE andNetscape Communicator return “Win32” for Windows 32-bit systems, “Mac68k” or “MacPPC” (depending on the processor)for Macintosh systems.It returns the actual name of the operating system for Unixsystems. On the other hand,Mozilla returns “Windows” for all Windows systems,“Macintosh” for allMacintosh systems,and “X11” for all Unix systems.So you have a lot of options to check for when checking the client platform. Checking for Windows and Macintosh systems is pretty straightforward; you just need to check for the various strings:

Because browsers return such varying values when a Unix platform is in use,it is necessary to make sure that the platform isn’t Windows or Macintosh, and then check for “X11” as well:

var isUnix = (navigator.platform == “X11”)&&!isWin && !isMac;

After you know what platform you are dealing with,you can try to determine which operating system isbeing used.

Detecting Windows operating systems

It seems like every other year a new version of the Windows operating system is released. For a long time, Microsoft has had two separate versions of Windows:one for home use and one for business use.The home use version was called simply called Windows.The business version was called Windows NT.

Little overlap occurred between the two versions.The one exception, however, was the user interface.In 2001, Microsoft decided to merge the home and business versions into a new product, Windows XP.This new operating system combines the stability and security of Windows NT with the user-friendliness of traditional Windows.

With that brief history lesson out of the way, many different versions of Windows are out there to detect:

  • Windows 95
  • Windows 98
  • Windows NT 4.0
  • Windows 2000
  • Windows ME
  • Windows XP

Luckily,the operating system information is included in the user-agent string, meaning that the browser gets to decide exactly what is displayed.The following table shows the different strings that are included in the user-agent string depending on the operating system being used:

Detecting Windows operating systems

This task begins just like the task of detecting browser versions, by defining some variables:

var isWin95 = isWin98 = isWinNT4 = isWin2K = isWinME = isWinXP =false;

In order to determine each version of Windows,you must check the user-agent string for each value in a row from the previous table.For example,if you want to check for Windows 98, you must check theuser-agent string for “Windows 98” and “Win98”, which covers all four browsers.

The easiest checks are for the Windows 95, 98, ME, 2000, and XP because they have only two values:

Windows NT 4.0 is a bit more complicated, and it is IE’s fault.It includes the string “Windows NT” only inits user-agent string,which means that you cannot search on it alone.Why? Because IE identifies Windows2000 as “Windows NT 5.0” and Windows XP as “Windows NT 5.1”.If you simply searched for “Windows NT”, the result would be true for all three, and that is not preferred behavior. So for Windows NT 4.0, you have to search for “Windows NT”, “WinNT”, “WinNT4.0”,and “Windows NT 4.0” and then make sure that it isn’t the other versions of Windows:

And there you have it.You have successfully detected the various Windows operating systems.

Detecting Macintosh operating systems

Believe it or not,this is actually an easy part of the trek into the client’s machine.Traditionally,Macintoshbrowsers would not tell you the operating system being used; the only information they provided was whether the Macintosh was using a 68000 processor or a PowerPC processor.Only recently have browsers begun to report the MacOS X as the operating system,meaning that testing for MacOS X is trustworthyif you find “MacOS X” in the user-agent string. If it’s not there,however, the user could still be usingMacOS X but not have a browser that reports it.For this reason,it’s best to stick to the old method of checking the processor.

The following table shows the strings that each browser includes in its user-agent string to indicate the processor being used:

Detecting Macintosh 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:

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 useragent 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 toinclude in the user-agent string.Thus,every browser shows the same string for the same operating system.

Here are some examples for SunOS:

Begin by defining a few variables representing the various versions you’re looking for:

Next,extract the operating system version by using a regular expression.Because SunOS uses the twodecimal approach,the expression looks similar to the one used with Mozilla:

var reSunOS = new RegExp(“SunOS (d+.d+(?:.d+)?)”);

After the version is extracted, you must use the compareVersions() function (explained in a previous section) to determine the minimum operating system versions:

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.


All rights reserved © 2018 Wisdom IT Services India Pvt. Ltd DMCA.com Protection Status

Java Script Topics