Skip to content

Instantly share code, notes, and snippets.

@jasonbellamy
Created November 7, 2012 19:35
Show Gist options
  • Save jasonbellamy/4033849 to your computer and use it in GitHub Desktop.
Save jasonbellamy/4033849 to your computer and use it in GitHub Desktop.
Simple JavaScript feature detection.
var testFeature = (function () {
return {
/**
* Accepts a feature name and feature test function to test for truthness
* @param {String} featureName of the feature being tested
* @param {Function} testMethod the feature test method ( this should always return true or false )
*/
addTest: function( featureName, testMethod ) {
if ( typeof featureName === 'string' && typeof testMethod === 'function' ) {
this.addClass( featureName, testMethod() );
}
},
/**
* Adds a class to the html element for each feature name
* the class is "featureName" if it's supported otherwise it's "no-featureName"
* @param {String} featureName name of the feature being tested
* @param {Bool} featureSupported is the feature supported?
*/
addClass: function( featureName, featureSupported ) {
document.documentElement.className += ' ' + ( featureSupported ) ? featureName : 'no-' + featureName;
}
};
}());
testFeature.addTest('fileinput', function () {
// feature tests logic goes in here and must return true or false.
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment