How to Detect Operating System in JavaScript
-
Detect Operating System With the
platform
Property in JavaScript -
Detect Operating System With the
userAgent
Property in JavaScript -
Detect Operating System With the
appVersion
Property in JavaScript
In JavaScript, the navigator
object returns the browser information. Technically, through the navigator properties, we send information about the browser and sometimes information about the app or operating system we are using to the server.
As a result, the server returns a string containing information about the browser from where the ping was received earlier.
Here, we will see the use of the platform
property with the navigator
object in further instances. We will also see how the navigator.userAgent
and navigator.appVersion
works.
Detect Operating System With the platform
Property in JavaScript
The platform
property is an almost obliterated feature. Most browsers do not receive the exact details of the browsers’ platform.
Usually, the basic functionality of this property is to detect the platform from where the browser is running.
We will get the correct operating systems name when using this property, but the main problem is that some browsers like Chrome, Firefox 63, Edge returns the version as 32 even if the actual version is 64. So, even if it is the easiest way to figure out the operating system, yet ignores this entropy.
The following code lines will explain the case.
Code Snippet:
var OS = navigator.platform;
console.log(OS);
Output:
As you can notice, we did receive the platforms’ name, but the real version is 64 of the PC from where the code is run. The browser we used is Chrome, and the returned message for version reflects 32.
Detect Operating System With the userAgent
Property in JavaScript
If we want to track a little detail about the user’s used browser and platform, we can go for the userAgent
property with the navigator
object. This special feature returns a string containing the browser’s name, version, and platform name.
Unlike the previous property (platform
), the userAgent
gives a definite outcome, including the version.
In the following example, we will try to match a pattern from the string and infer it to its correct operating system.
Code Snippet:
var OS = 'Unknown';
if (navigator.userAgent.indexOf('Win') != -1) OS = 'Windows';
if (navigator.userAgent.indexOf('Mac') != -1) OS = 'MacOS';
if (navigator.userAgent.indexOf('X11') != -1) OS = 'UNIX';
if (navigator.userAgent.indexOf('Linux') != -1) OS = 'Linux';
console.log(OS);
console.log(navigator.userAgent);
Output:
Detect Operating System With the appVersion
Property in JavaScript
In the case of the appVersion
property, we also retrieve the information about the browser and its platform. It is also recommended to know your PC’s operating system via JavaScript.
Yet, it serves a minimal purpose. You can have a sneak peek at this article for more info.
Also, this feature is a read-only property.
Code Snippet:
var OS = 'Unknown';
if (navigator.appVersion.indexOf('Win') != -1) OS = 'Windows';
if (navigator.appVersion.indexOf('Mac') != -1) OS = 'MacOS';
if (navigator.appVersion.indexOf('X11') != -1) OS = 'UNIX';
if (navigator.appVersion.indexOf('Linux') != -1) OS = 'Linux';
console.log(OS);
Output: