Capability detection refers to checking a web-browser and collecting information of the features it supports (or doesn't). It is different from browser detection.

Upon a web page loading, a developer's script executes and verifies what the set of methods/properties need to be tested for support from the browser. The Boolean result of its supported or not can be assigned to a variable. Throughout the web-page the variable can be accessed before executing the code. This way, developer can write a set of code in if () statement for feature supported and set of code in else {} statements for feature not supported. Few examples of capability detection code are given below:

Example #1 for capabilities detection

//Check for browser if it has Netscape-style plugins
var hasNSPlugins = !!(navigator.plugins && navigator.plugins.length);
if(hasNSPlugins){
    //If it has plugins execute this code
}
else{
    //If there is no plugin support execute here
}

Example #2 for capabilities detection

//check if the browser has basic DOM Level 1 capabilities
var hasDOM1 = !!(document.getElementById && document.createElement &&
document.getElementsByTagName);
if(hasDOM1){
    //If it has DOM 1 execute this code
}
else{
    //If there is no DOM support execute here
}

Example #3 for capabilities detection

//Check if browser supports “canvas”
var element = document.createElement(“canvas”);
var chk = element.getContext && element.getContext(“2d”);
if(chk){
    //Canvas element present, execute this code
}
else{
    //Else run this code.
}

 

›› go to examples ››