Created
November 7, 2012 19:35
-
-
Save jasonbellamy/4033849 to your computer and use it in GitHub Desktop.
Simple JavaScript feature detection.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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