Skip to content

Instantly share code, notes, and snippets.

@arozwalak
Last active March 9, 2017 22:08
Show Gist options
  • Save arozwalak/7468966 to your computer and use it in GitHub Desktop.
Save arozwalak/7468966 to your computer and use it in GitHub Desktop.
Javascript: Feature detection
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Feature Detection</title>
</head>
<body>
<input type="text" placeholder="John Doe">
<input type="email">
<script>
if (!'placeholder' in document.createElement('input')) {
// a polyfill
}
function testSupport (type) {
var i = document.createElement('input');
i.setAttribute('type', type);
return i.type === type;
}
if ( !testSupport('email')) {
// provide fallback
}
if (document.createElement('canvas').getContext) {
// browser supports canvas
}
if (document.createElement('audio').canPlayType) {
// browser supports audio
}
if (document.createElement('video').canPlayType) {
// browser supports video
}
function supportsStorage() {
try {
return 'localStorage' in window && window.localStorage !== null;
} catch(e) {
return false;
}
}
if (!!navigator.geolocation) alert('track me!');
</script>
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment