Browser detection versus feature detection

前端 未结 3 1884
半阙折子戏
半阙折子戏 2020-11-22 07:56

I am going to play a devil\'s advocate for a moment. I have been always wondering why browser detection (as opposed to feature detection) is considered to be a flat out as a

3条回答
  •  迷失自我
    2020-11-22 08:55

    Here's a good article explaining how feature detection is superior in so many ways to browser sniffing.

    The truth is that sniffing is extremely fragile. It's fragile in theory, as it relies on an arbitrary userAgent string and then practically maps that string to a certain behavior. It's also fragile in practice, as time has shown. Testing every major and minor version of dozens of browsers and trying to parse build numbers of some of those versions is not practical at all; Testing certain behavior for quirks, on the other hand, is much more robust. Feature tests, for example, often catch bugs and inconsistencies that browser vendors incidentally copy from each other.

    From my own experience, fixing Prototype.js in IE8, I know that 90% of the problems could have been avoided if we didn't sniff in the first place.

    While fixing Prototype.js I discovered that some of the features that need to be tested are actually very common among JS libraries, so I made a little collection of common feature tests for anyone willing to get rid of sniffing.

提交回复
热议问题