Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine browser platform without navigator.platform

While previously we were able to use navigator.platform in order to check which OS a browser was running in (which is still necessary for things like key event translation, e.g. Home vs. metaKey+ArrowLeft, or placing virtual modal "close" icons in a way that follows OS conventions) that option no longer exists. (That is to say, right now it technically still does but it's no longer a solution that is guaranteed to work by the time you deploy your code).

I can't seem to find any official documentation on what the replacement is supposed to be if OS knowledge is required: what is the new and improved way to determine which OS/platform a web page is running on?

(The currently most popular SO question around this, "Best way to detect Mac OS X or Windows computers with JavaScript or jQuery", does not have any answers that have been edited since the decision to deprecate navigator.platform was made, so unfortunately is not useful at the moment)

like image 225
Mike 'Pomax' Kamermans Avatar asked Dec 19 '25 23:12

Mike 'Pomax' Kamermans


1 Answers

2025 Edit:

As it turns out, the WhatWG spec for navigator.platform does not (and may never have) list(ed) it as deprecated, and the original Chromium announcement was changed from "removal" to "reduced information" since their original blog post, so using navigator.platform should be fine (as long as you only need to know which OS you're in. Not "which version of that OS" or in the case of linux, which of the million possible flavours), and will work in any spec-compliant browser, which is pretty much any browser you can think of. Yes, even that weird one. Correct: because it's just another reskinned chrome.

Original answer:

It turns out that the only realistic option for now is to look at the User Agent string and make decisions based on that (either by handrolling code, or using a user agent parsing library that does that for you).

This is not ideal, but as the goal is OS detection, and explicitly not browser detection, it's also not too hacky or liable to break in the future.

like image 184
Mike 'Pomax' Kamermans Avatar answered Dec 21 '25 12:12

Mike 'Pomax' Kamermans



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!