Deprecated Javascript OS detection techniques
Asked Answered
R

3

13

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.

Rhaetian answered 21/7, 2016 at 14:8 Comment(15)
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.Lucie
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.Archduchy
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.Antistrophe
Just FWIW: There's nothing in the spec 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.Leisha
@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]).Leisha
@T.J.Crowder: And for that, you could just as easily do it server-side, which I'm guess will still have userAgent.Drier
@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.Leisha
@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.jsAntistrophe
In fact, MDN doesn't say navigator.platform is being deprecated: developer.mozilla.org/en-US/docs/Web/API/NavigatorID/platformLeisha
@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... :-)Leisha
@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.Drier
I think we're all on the same page, here.Drier
@Antistrophe 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.Rhaetian
(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. :-) )Leisha
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.Leisha
J
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

Johnston answered 1/12, 2017 at 19:5 Comment(1)
To update for 2023, all of the keys OP mentions (besides userAgent) are now in fact deprecated, according to MDN: developer.mozilla.org/en-US/docs/Web/API/Navigator/platformPoliard
P
-1

You can use:

navigator.userAgentData.platform
Poky answered 16/12, 2021 at 14:57 Comment(1)
Not supported in most browsers (Firefox for example). See developer.mozilla.org/en-US/docs/Web/API/Navigator/….Asseveration
D
-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.

Drier answered 21/7, 2016 at 14:21 Comment(2)
I want to display an icon in Apple platforms and another one in Android platforms. I think it's a legit use case.Spec
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 respectivelyPoliard

© 2022 - 2024 — McMap. All rights reserved.