This tutorial will cover the basic theory and technique of browser detection in JavaSript enabled web browsers.
The JavaScript that will detect the browser will use navigator object. This object holds these listed variables.
The navigator.userAgent property is more better than appName because, for example, Firefox (and few other browsers) may return the string "Netscape" as the value of navigator.appName for compatibility with Netscape Navigator.
Following Script will cover the all method and properties of browser detection scheme.
Listing 1: Script for detecting browser Name Version /Full version
<html>
<head>
<title>Browser Detection</Title>
<Script Language="JavaScript">
var objappVersion = navigator.appVersion;
var objAgent = navigator.userAgent;
var objbrowserName = navigator.appName;
var objfullVersion = ''+parseFloat(navigator.appVersion);
var objBrMajorVersion = parseInt(navigator.appVersion,10);
var objOffsetName,objOffsetVersion,ix;
// In Chrome
if ((objOffsetVersion=objAgent.indexOf("Chrome"))!=-1) {
objbrowserName = "Chrome";
objfullVersion = objAgent.substring(objOffsetVersion+7);
}
// In Microsoft internet explorer
else if ((objOffsetVersion=objAgent.indexOf("MSIE"))!=-1) {
objbrowserName = "Microsoft Internet Explorer";
objfullVersion = objAgent.substring(objOffsetVersion+5);
}
// In Firefox
else if ((objOffsetVersion=objAgent.indexOf("Firefox"))!=-1) {
objbrowserName = "Firefox";
}
// In Safari
else if ((objOffsetVersion=objAgent.indexOf("Safari"))!=-1) {
objbrowserName = "Safari";
objfullVersion = objAgent.substring(objOffsetVersion+7);
if ((objOffsetVersion=objAgent.indexOf("Version"))!=-1)
objfullVersion = objAgent.substring(objOffsetVersion+8);
}
// For other browser "name/version" is at the end of userAgent
else if ( (objOffsetName=objAgent.lastIndexOf(' ')+1) <
(objOffsetVersion=objAgent.lastIndexOf('/')) )
{
objbrowserName = objAgent.substring(objOffsetName,objOffsetVersion);
objfullVersion = objAgent.substring(objOffsetVersion+1);
if (objbrowserName.toLowerCase()==objbrowserName.toUpperCase()) {
objbrowserName = navigator.appName;
}
}
// trimming the fullVersion string at semicolon/space if present
if ((ix=objfullVersion.indexOf(";"))!=-1)
objfullVersion=objfullVersion.substring(0,ix);
if ((ix=objfullVersion.indexOf(" "))!=-1)
objfullVersion=objfullVersion.substring(0,ix);
objBrMajorVersion = parseInt(''+objfullVersion,10);
if (isNaN(objBrMajorVersion)) {
objfullVersion = ''+parseFloat(navigator.appVersion);
objBrMajorVersion = parseInt(navigator.appVersion,10);
}
document.write(''
+'Browser name = '+objbrowserName+'<br>'
+'Full version = '+objfullVersion+'<br>'
+'Major version = '+objBrMajorVersion+'<br>'
+'navigator.appName = '+navigator.appName+'<br>'
+'navigator.userAgent = '+navigator.userAgent+'<br>'
)
</script>
</head>
<body>
</body>
</html>

Figure 1 Above figure is output of browser detection for chrome.

Figure 2 Above figure is output of browser detection for Mozilla Firefox.
This tutorial begins with browser detection theory and later it had explain the browser detection scheme and script for detection. Browser detection is very useful in current environment because the application should support all kind of browsers. So detect the browser and then write the corresponding code.
As usual, Please comment here if you have any questions.








See the prices for this post in Mr.Bool Credits System below: