CAPABILITY DETECTION
Capability detection (also called feature detection) uses a suite of simple checks in the browser's JavaScript runtime to test for support of various features. This approach presumes that specific browser knowledge is unnecessary and that the solution may be found by determining if the capability in question actually exists. The basic pattern for capability detection is as follows:
if (object.propertyInQuestion) {
// use object.propertyInQuestion
}
For example, the DOM method document.getElementById()
didn't exist in Internet Explorer prior to version 5, although the same functionality could be achieved using the nonstandard document.all
property. This led to a capability detection fork such as the following:
function getElement(id) {
if (document.getElementById) {
return document.getElementById(id);
} else if (document.all) {
return document.all[id];
} else {
throw new Error("No way to retrieve element!");
}
}
The purpose of...