43

How to detect MacOS X, iOS, Windows, Android and Linux operating system with JavaScript?

Vladyslav Turak
  • 5,386
  • 3
  • 22
  • 25

1 Answers1

178

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 (!os && /Linux/.test(platform)) {
    os = 'Linux';
  }

  return os;
}

alert(getOS());

Inspiration:

  1. What is the list of possible values for navigator.platform as of today?
  2. Best way to detect Mac OS X or Windows computers with JavaScript or jQuery
  3. How to detect my browser version and operating system using JavaScript?
  4. 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:

  1. List of all Mobile Browsers
  2. List of all Browsers

This code works properly. I tested it on all the OS: MacOS, iOS, Android, Windows and UNIX, but I can't guarantee 100% sure.

Ghyath Darwish
  • 2,178
  • 4
  • 11
  • 28
Vladyslav Turak
  • 5,386
  • 3
  • 22
  • 25
  • 3
    Why not just use "Mac" for MacOS? Should be more future proof. – Aseem Gautam Feb 04 '19 at 10:11
  • Nice solution! Good job! :) – Aleksandar Jun 12 '19 at 09:27
  • 4
    You 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
  • 2
    Why 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
  • 3
    Why do you start the last elseif with `!os && `? – Gust van de Wal May 13 '20 at 20:02
  • 1
    `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
  • 1
    Isn'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
  • Navigator.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. – MrParrot Dec 31 '21 at 15:30