Replacing the browser’s User Agent

HAHAHA, that’s easy. Just use feature detection! Problem solved!

Correct, feature detection (think Modernizr) can effectively replace the browser’s User Agent (UA), but sadly, not everything is peaches and ice cream with feature detection. There are often legitimate use cases that require the UA and cannot be performed using feature detection. I of course, refer to collecting the browser’s name, version number, platform name (and possibly version), and layout engine details for the purpose of displaying them in a message to the user or for visitor browser analysis. Try getting that information with feature detection. You cannot. Sniffing the user agent using various methods is absolutely required to get that information.

The problem is a browser’s user agent lies all the time, some worse than others. This lying is nothing new, browsers have done it for years on end, and has now reached the point that the IE team is systemically spoofing their UA in order to stop legacy code and modern devs who dislike IE from serving incorrect and outdated IE-only hacks while giving standards-complaint code to every other browser. Users can download and install plugins that change the browser’s UA. Bots (both good and bad) can provide different UAs. The lying has reached the point that the creator of HTML5Test has written an extensive PHP library that attempts to correctly detect every browser. You can imagine that is a monumental task that only gets harder over time.

So while web developers the world over denounces user agent sniffing in favor of feature detection to check browser compatibility, developers who need legitimate access to the browser, platform, and layout engine name/version are being left out in the cold. Also, did I mention there are extremely rare cases where UA sniffing is the only way to check for/about something? In that case, what can be done? There is always window.navigator, but that is a completely unreliable source.

Maybe I am barking up a mute tree or throwing around an oft-suggested but ignored idea, but what if the W3C or WHATWG proposed a new browser object that accurately reported all the information developers need to identify the browser? I’d love to see something similar to this:

window.navigator.browser = {
  name: "Mozilla Firefox",
  version: "33.1",
  engineName: "Gecko",
  engineVersion: "33.1",
  platformName: "Micosoft Windows",
  platformVersion: "7"

This would be a simple, read-only JavaScript object that reports the browser name and version, layout engine name and version, and finally platform name and version, each one a string type for consistency. Such an object is easy to access and can be used to accurately identify the browser without having to haggle with the dreaded UA.

Of course, even the most responsible developers sees the downsides to such an object. By proposing, standardizing, and implementing this, we revert right back to pre-feature detection days, enabling devs to use browser-specific hacks that are not needed or to serve different browsers different content. But face it, that danger is sadly never going away. There will always be developers who use such frowned upon, deceiving ways no matter what. I am sure even some strategic feature detection checks could be used to do the same things. That is why our job as responsible developers to teach the correct and proper ways on this topic. Our work has paid off too; Mondernizr is now the second most popular JavaScript library. However, web browser usage seems to be headed towards a fully anonymous state, which is good for development but bad for statistics and analytics. However, by the time this proposed object would be standardized and fully implemented, hopefully feature detection would be so ingrained in our development process that only rogue developers would use it for the old, unfair ways, and legacy code would never know such an object exists and thus would never access it (not to mention legacy UA sniffing code will be 100% broken long before this, if the spoofing trends continue).

Maybe I am in a minority here and/or might be thinking too much, but the Web still needs a way to correctly identify the browser being used for statical usage without resorting to crazy hacks and UA sniffing messes. The battle to drop UA sniffing and switch to feature detection has mostly been won. Now it is time to rally to support browser identification for statistical and information purposes, because if the Web is fully anonymous, how will developers, users, and companies know what browser is on top of the usage charts, and if it is feasible to blindly continue development and investing resources of/for a browser that possibly only has 1% of the usage base?