How to detect MacOS X, iOS, Windows, Android and Linux operating system with JavaScript?
Asked
Active
Viewed 8.7k times
1 Answers
207
I learnt a lot about window.navigator
object and its properties: platform
, appVersion
and userAgent
. To my mind, it's almost impossible to detect user's OS with 100% sure, but in my case 85%-90% was enough for me.
So, after examining tons of the stackoverflows' answers and some articles, I wrote something like this:
function getOS() {
var userAgent = window.navigator.userAgent,
platform = window.navigator?.userAgentData?.platform || window.navigator.platform,
macosPlatforms = ['Macintosh', 'MacIntel', 'MacPPC', 'Mac68K'],
windowsPlatforms = ['Win32', 'Win64', 'Windows', 'WinCE'],
iosPlatforms = ['iPhone', 'iPad', 'iPod'],
os = null;
if (macosPlatforms.indexOf(platform) !== -1) {
os = 'Mac OS';
} else if (iosPlatforms.indexOf(platform) !== -1) {
os = 'iOS';
} else if (windowsPlatforms.indexOf(platform) !== -1) {
os = 'Windows';
} else if (/Android/.test(userAgent)) {
os = 'Android';
} else if (/Linux/.test(platform)) {
os = 'Linux';
}
return os;
}
alert(getOS());
Inspiration:
- What is the list of possible values for navigator.platform as of today?
- Best way to detect Mac OS X or Windows computers with JavaScript or jQuery
- How to detect my browser version and operating system using JavaScript?
- How to detect Browser and Operating System Name and Version using javaScript
Also I used the lists of mobile and desktop browsers to test my code:
This code works properly. I tested it on all the OS: MacOS, iOS, Android, Windows and UNIX, but I can't guarantee 100% sure.

user3347937
- 3
- 2

Vladyslav Turak
- 6,014
- 4
- 24
- 25
-
3Why not just use "Mac" for MacOS? Should be more future proof. – A G Feb 04 '19 at 10:11
-
Nice solution! Good job! :) – Aleksandar Jun 12 '19 at 09:27
-
4You should add `"darwin"` to `macosPlatforms` – William Aug 31 '19 at 11:53
-
I think you need to use userAgent instead of platform for Chrome OS, because in Chrome OS the platform is Linux – ZYinMD Nov 29 '19 at 05:49
-
2Why do you say "I've wrote something like this" ? I wonder what the actual code do you use and why does it differ from the one here. – okram Feb 03 '20 at 17:12
-
3Why do you start the last elseif with `!os && `? – Gust van de Wal May 13 '20 at 20:02
-
3`iosPlatforms.indexOf(platform) !== -1` Can be written more distinctly as `iosPlatforms.includes(platform)`. It also looks like you could use string matching instead to be more generic `platform.toLowerCase.startsWith("win")`. You can also eliminate mutating variables by using return in each if conditions (`return 'Windows'` ). – blindguy Feb 26 '21 at 17:07
-
When running on an iPad pro in emulator this returns Mac OS. – maxisme May 14 '21 at 11:02
-
3Isn't time to update this since "navigator.platform" is deprecated? https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID/platform – MarketerInCoderClothes Jun 15 '21 at 08:20
-
1Navigator.userAgentData is still not fully supported - it doesn't work on Firefox, Safari or Internet Explorer. It seems slightly ironic that it says this on Mozilla's website. – TheKodeToad Dec 31 '21 at 15:30
-
As of OS X Monterey window.navigator?.userAgentData?.platform returns 'macOS'. – tashicorp Sep 29 '22 at 04:40
-
return null for M1 MBP – GorvGoyl Jan 26 '23 at 11:01