Skip to content

Instantly share code, notes, and snippets.

@everget
Last active June 1, 2018 18:19
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save everget/ca50e5cd7073a1d8d411c2aa9290fb04 to your computer and use it in GitHub Desktop.
Save everget/ca50e5cd7073a1d8d411c2aa9290fb04 to your computer and use it in GitHub Desktop.
Summary of JavaScript feature detection techniques

Member in object

Check whether a certain method or property (typically an entry point into using the API or other feature you are detecting for) exists in its parent Object

Check if a certain property exists on a global object (such as window or navigator)

if("geolocation" in navigator) { ... }

Property on element

Create an element, then check if a certain property exists on that element.

Create an element in memory using Document.createElement() and then check if a property exists on it. The example shown is a way of detecting HTML5 Canvas support

function supports_canvas() {
  return !!document.createElement('canvas').getContext;
}

if(supports_canvas()) { ... }

Method on element return value

Create an element in memory using Document.createElement() and then check if a method exists on it. If it does, check what value it returns

Create an element, check if a certain method exists on that element, then call the method and check the value it returns

Property on element retains value

Create an element in memory using Document.createElement(), set a property to a certain value, then check to see if the value is retained

Create an element, set a property to a certain value, then check if the property has retained its value.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment