If window.navigator.userAgent is out of date, what should I use instead?

MDN documentation says window.navigator.userAgent is outdated and should not be used. If I want to collect user browser and os data for analytics (not feature detection), what should I use instead?

+6
source share
2 answers

The user agent string becomes meaningless and extremely unreliable.

You should not use the user agent string, use the discovery function instead. If you need to use function X, check if there is X.

But to answer your question directly, there is no alternative to JS.

+3
source

Browser authentication based on user agent string detection is unreliable and not recommended because the user agent string is user configurable.

For instance:

  • In Firefox, you can change the preference general.useragent.override to something like: config. Some Firefox extensions do that; however, this only changes the HTTP header that is sent, and does not affect browser detection that is executed by JavaScript code.
  • Opera 6+ allows users to set the browser identification line through the menu
  • Microsoft Internet Explorer uses the Windows registry
  • Safari and iCab allow users to change the browser user agent string to predefined Internet Explorer or Netscape strings through menus.

Source

I think they are trying to completely remove this function from JavaScript.

Update:

Object Oriented JavaScript, 2nd Edition : It's best not to rely on the user agent string, but to use the sniffing function (also called feature discovery). The reason for this is because it is difficult to track all browsers and their different versions. It is much easier to just check if the function you are going to use is really available in the user's browser. For example, take a look at the following code:

if (typeof window.addEventListener === 'function') { // feature is supported, let use it } else { // hmm, this feature is not supported, will have to // think of another way } 
+2
source

All Articles