12

I was wondering why javascript os detection techniques like navigator.userAgent, navigator.appName, navigator.appVersion and navigator.platform are in process of being dropped from web standards.

https://developer.mozilla.org/en-US/docs/Web/API/Navigator

If you visit every of those navigator props, you can see

Deprecated

This feature has been removed from the Web standards. Though some browsers may still support it, it is in the process of being dropped. Do not use it in old or new projects. Pages or Web apps using it may break at any time.

So I would like to know

  1. Why they're removing this
  2. Will they introduce a new way for OS detection
  3. Can I use these techniques even if they're deprecated.

Probably there is a lot of cases where we need to know OS version.

ardoc
  • 121
  • 1
  • 1
  • 5
  • 2
    while this is a good question, this probably isn't the right place to ask it. Aside from not having a problem to be solved, it's also opinionated, as no single person other than those responsible for drafting the web standards can tell you their reasoning. It also is on the edge of being a rant in disguise. – Claies Jul 21 '16 at 14:13
  • Probably because they have never provided reliable information, and get less and less reliable every year. For example, every modern browser's `navigator.appName` is "Netscape" which has zero information value. – JJJ Jul 21 '16 at 14:13
  • Why would you need to detect this stuff? Sure you know your code works in this browser, but not that browser -- but what happens if somebody visits from some obscure browser you code hasn't planned for? With web standards gaining traction this technique is obsolete. – JKirchartz Jul 21 '16 at 14:13
  • Just FWIW: There's nothing [in the spec](https://html.spec.whatwg.org/multipage/webappapis.html#dom-navigator-platform) to suggest that `navigator.platform` is being deprecated. Other than MDN (which is community-edited and therefore sometimes incorrect, if generally very good), do you have any reference for that? `appName` and `appCodeName` are (the spec says they should always be `"Netscape"` and `"Mozilla"`, respectively), but not `platform` as far as I've heard. – T.J. Crowder Jul 21 '16 at 14:16
  • 2
    @JKirchartz: The only real (and not obsolete) use-case I've ever seen for platform detection is when offering downloadable software: With detection, you can default to the platform (say, Linux) the user is viewing your page with (while still letting them navigate to other platforms [say, Windows]). – T.J. Crowder Jul 21 '16 at 14:17
  • 1
    @T.J.Crowder: And for that, you could just as easily do it server-side, which I'm guess will still have userAgent. – Whothehellisthat Jul 21 '16 at 14:22
  • @Whothehellisthat: But you can't count on user-agent, it can be spoofed (which, to be fair, isn't that big a deal -- if they want to lie to you, that's their problem). More importantly, it requires per-response server-side programming, whereas for high-volume sites, you want to be able to use a static resource on a CDN. – T.J. Crowder Jul 21 '16 at 14:24
  • @T.J.Crowder you can't count on `navigator.userAgent` either for the same reasons, in firefox you can change the preference general.useragent.override in about:config. -- browser detection is a bad idea, there's just too many browsers, the best practice is to use feature detection with something like Modernizr.js – JKirchartz Jul 21 '16 at 14:26
  • In fact, MDN **doesn't** say `navigator.platform` is being deprecated: https://developer.mozilla.org/en-US/docs/Web/API/NavigatorID/platform – T.J. Crowder Jul 21 '16 at 14:26
  • @JKirchartz: I didn't say anything about using `userAgent`. :-) It's `navigator.platform` that seems (very, very occasionally) useful. I also didn't say anything about doing browser detection. If you want to tell me how to *feature*-detect the OS, by all means, I'd be keen to know... :-) – T.J. Crowder Jul 21 '16 at 14:27
  • @T.J.Crowder: Right. Though the user could just get a redirection from the server with little effort. And as you said, the use-case for it is pretty narrow anyway. – Whothehellisthat Jul 21 '16 at 14:27
  • 1
    I think we're all on the same page, here. – Whothehellisthat Jul 21 '16 at 14:29
  • @JKirchartz I'm working on project that involves a lot of styling, visualizations and animations. It needs to be cross browser responsive, with attention to details. Problem is that on windows 10, mozilla, I get few deviations which can't be solved simply by editing existing CSS because it will affect other browsers. And i dont know really why it's rendered different on Win 10 while on Win 8.1 it's ok, on every browser. – ardoc Jul 21 '16 at 14:33
  • (`navigator.platform` can also be spoofed trivially in firefox, in about:config, via the general.platform.override setting. I guessed at that name from the general.useragent.override setting. :-) ) – T.J. Crowder Jul 21 '16 at 14:33
  • 2
    I'm voting to close this question as off-topic because the premise is that **OS** detection aspects of `navigator` are deprecated, but neither the spec nor MDN indicates that the primary OS-related property, `navigator.platform`, is in fact being deprecated. – T.J. Crowder Jul 21 '16 at 14:47

3 Answers3

2

It was incorrectly or accidentally marked as deprecated on MDN. They quickly corrected the page once they saw the problem, but since it had been a definitive source, references to it being deprecated still exist here and there.

Here's the conversation where it was fixed: https://groups.google.com/forum/#!topic/mozilla.dev.mdc/tIx2iiH2u3o

IKM
  • 147
  • 1
  • 5
  • 3
    To update for 2023, all of the keys OP mentions (besides `userAgent`) are now in fact deprecated, according to MDN: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/platform – Allie Howe May 23 '23 at 21:58
0

You can use:

navigator.userAgentData.platform
Tomerikoo
  • 18,379
  • 16
  • 47
  • 61
Temo Kiknadze
  • 269
  • 3
  • 8
  • 1
    Not supported in most browsers (Firefox for example). See https://developer.mozilla.org/en-US/docs/Web/API/Navigator/userAgentData#browser_compatibility. – Zacharya Haitin Jul 21 '22 at 12:34
-5

I think the general thinking is that it's becoming unnecessary. Theoretically at least, there shouldn't be any functionality that works differently in any browser vs any other browser--at least not for JavaScript.

What you usually browser sniff for are features, and there are plenty of ways to find most of that stuff out without having to infer anything from the nightmare that is userAgent.

So it may be deprecated, or it may not. But it's a good idea to not have to sniff the browser. That stuff can get real complicated real fast. Even if it does become deprecated though, it'll probably stick around for a few decades so that half the web that still relies on it doesn't crash and burn.

Whothehellisthat
  • 2,072
  • 1
  • 14
  • 14
  • I want to display an icon in Apple platforms and another one in Android platforms. I think it's a legit use case. – Ramon Balthazar Jan 20 '23 at 14:54
  • Disagree - another use case is for keyboard shortcuts: on a KeyboardEvent, `metaKey` flag is Mac's Command key but Windows' WinKey. Normally you'd want Ctrl + S and Cmd + S to both save, on Windows and Mac respectively – Allie Howe May 23 '23 at 21:53