Implementing Feature Detection
Implementing Feature Detection
Feature detection involves working out whether a browser supports a certain block of
code, and running different code depending on whether it does (or doesn't), so that the
browser can always provide a working experience rather than crashing/erroring in some
browsers. This article details how to write your own simple feature detection, how to use a
library to speed up implementation, and native features for feature detection such as
@supports .
Prerequisites: Familiarity with the core HTML, CSS, and JavaScript languages; an idea of the
high-level principles of cross-browser testing.
Objective: To understand what the concept of feature detection is, and be able to implement
suitable solutions in CSS and JavaScript.
if ("geolocation" in navigator) {
navigator.geolocation.getCurrentPosition(function (position) {
// show the location on a map, such as the Google Maps API
});