Browser detection via object/feature detection

2019-04-15 20:47发布

I know that browser detection using the userAgent property is a terrible idea because browsers will lie to you and because different versions of the same browser may have different feature sets with the same value for userAgent. Feature detection is definitely the way to go for actually figuring out whether what you want to do is actually possible in a browser.

What I'm hoping to figure out is if it is possible to use feature detection to figure out what the browser actually is. That is to say, identifying the particular quirks that a user agent has and then determining which browser has those exact same quirks. For example, if a browser does not define XMLHttpRequest but does define ActiveXObject then we know that it is going to be MSIE 6 or below.

Is there a library of such identifying quirks or a listing of them so I can write my own?

Edit: I didn't really provide a rationale behind this. I'd like to upgrade a system that collects usage statistics for a web application. It requires me to figure out the browser and currently uses userAgent. I'm looking for something more robust.

3条回答
戒情不戒烟
2楼-- · 2019-04-15 21:25

It's a matter of horses for courses. Browser sniffing by UA string was considered flawed because it made assumptions about browser features based identifying a browser from an arbitrary string of text. It was flawed not only because UA strings are notoriously unreliable, but because the analysis of the string was limited to those browsers that were known about. Introducing a new browser, or even a new version of a known browser, required updating of the browser sniffing part of the program.

On the other hand, trying to determine the browser from features is even less reliable. You'll end up trying to define a series of tests for browser quirks for each individual browser. You will have a never ending job trying to work out which browsers have which features to the exclusion of all other browsers.

Sites that collect statistics do it at the server using the user-agent header. Of course it is unreliable, but results are at least able to be analysed to within a reasonable error, new or unknown browsers can be identified much more easily and you aren't reliant on client-side scripting.

There are huge databases of UA strings that can be used for analysis, e.g. http://www.user-agents.org/. Just note that you will have a certain error rate and don't rely on the stats too heavily (user-agent.org lists 24 UA strings with "mozilla" in them, most of which are not browsers, there are 174 with MSIE in them).

查看更多
\"骚年 ilove
3楼-- · 2019-04-15 21:27

Modernizr is a good library for that and combined with YepNope you'll get all you need to make websites that work well in all (well most) browsers.

查看更多
干净又极端
4楼-- · 2019-04-15 21:31

Now that you've clarified what you're really looking for (recording of actual browser type for statistical gathering, not web page programming), it seems that the default out-of-the-box behavior for all browsers contains a non-deceptive and uniquely identifying user agent. So, for first order of magnitude, you should make sure you understand how to interpret all of those correctly since that will be probably 99% of what you encounter. Google shows a bunch of different lists of those user agents around. I like the "browser" column of this one. You can fairly easily identify the rendering engine and version in many of the browsers (Gecko, Webkit, MSIE, Presto) and then decide how much more detail you care about (e.g. whether it's Firefox or Camino) from there. Every browser is uniquely identified out of the box. You can program in as much level of detail sub-browser identification as you have time for.

Then, I'd be surprised if it's really worth it to try to identify the browsers that are lying to you with a false user-agent. If you have 30 different version releases of 50 different browsers, that's an enormous amount of work to try to uniquely identify each one via feature detection without relying on the user agent. You'd have to build an enormous database of test that you were regularly updating. And, I have no idea how you'd even get ahold of all those minor revisions to figure out how to code for them and/or test for them.

If you just want to identify which browsers are lying about their rendering engine (claiming Gecko when they are really MSIE, for example) then that's probably a simpler issue as you can get by with a smaller number of feature tests (such as the one you identified for IE6). But, that's still a tall order. If I were doing this, I'd start with a set of proprietary things that IE supports in each major version and test for those. It's generally safe to say that nobody else supports most IE specific things that have no likelihood of being a standard. Then, I'd probably look for some of the mozilla specific things in the Gecko rendering engine. Right now, every engine has their own CSS3 specific tags -moz, -o, -ms, -webkit which pretty much nails which rendering engine it is (for the later versions). Those will eventually fade when those standards are finalized, but most browsers will retain backward compatibility with their variant for a long time (Microsoft, probably forever).

You can probably study some of the cross-browser libraries that use heavy feature detection like Modernizer, YUI, jQuery, etc... for other ideas.

查看更多
登录 后发表回答