Chapter 3. Lay a Foundation for the Future with Feature Detection and Polyfills

I am the last and highest court of appeal in detection.

Sherlock Holmes, The Sign of the Four

One of the greatest challenges in developing modern websites is managing the broad range of device and browser capabilities present in the current ecosystem. One of the core ways to do this is via feature detection, testing for the presence of specific web platform features. This is compared to doing browser detection by looking for specific characteristics in the user agent string and coding for some specific version of Mobile Safari or Internet Explorer. That’s the way people used to do things. There are still some use cases for doing browser detection (one is actually covered in the next chapter), but most of the time you want to think about feature detection, which is the concern of this chapter and is the more commonly recommended approach in modern web development.

Although you can roll your own solutions, the best option for feature detection on the modern Web is with an open source project called Modernizr. Modernizr is a feature-detection library that makes it easy to test for dozens of web platform features, with a catalog of features growing by one or two a month. This chapter will look at feature detection in general, introduce Modernizr, show you how to download and customize the library, and illustrate three common patterns for leveraging Modernizr’s feature detection in order to smoothly work ...

Get The Uncertain Web now with the O’Reilly learning platform.

O’Reilly members experience live online training, plus books, videos, and digital content from nearly 200 publishers.