Detection Techniques
When your browser renders a web page, it constructs a
Document Object Model (DOM), a collection of objects that represent the
HTML elements on the page. Every elementâevery <p>
, every <div>
, every <span>
âis represented in the
DOM by a different object. (There are also global
objects, like window
and document
, that arenât tied to specific
elements.)
All DOM objects share a set of common properties, but some objects have more than others. In browsers that support HTML5 features, certain objects will have unique properties. A quick peek at the DOM will tell you which features are supported.
There are four basic techniques for detecting whether a browser supports a particular feature. From simplest to most complex:
Check if a certain property exists on a global object (such as
window
ornavigator
).For an example of testing for geolocation support, see Geolocation.
Create an element, then check if a certain property exists on that element.
For an example of testing for canvas support, see Canvas.
Create an element, check if a certain method exists on that element, then call the method and check the value it returns.
For an example of testing which video formats are supported, see Video Formats.
Create an element, set a property to a certain value, then check if the property has retained its value.
For an example of testing which
<input>
types are supported, see Input Types.
Get HTML5: Up and Running now with the O’Reilly learning platform.
O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.