eTechStack | Computer & Technology Guide

Javascript browser detection

How to detect browser using javascript? Here is a javascript to detect browser.



var ua = navigator.userAgent;
var browserName  = navigator.appName;
var nVer = navigator.appVersion;
var fullVersion  = ''+parseFloat(navigator.appVersion); 
var majorVer = parseInt(navigator.appVersion,10);
var nameOffset,verOffset,ix;

//Google Chrome 
if ((verOffset=ua.indexOf("Chrome"))!=-1) {
 browserName = "Chrome";
 fullVersion = ua.substring(verOffset+7);
}
// Opera 15+, 
else if ((verOffset=ua.indexOf("OPR/"))!=-1) {
 browserName = "Opera";
 fullVersion = ua.substring(verOffset+4);
}

// older Opera,
else if ((verOffset=ua.indexOf("Opera"))!=-1) {
 browserName = "Opera";
 fullVersion = ua.substring(verOffset+6);
 if ((verOffset=ua.indexOf("Version"))!=-1) 
   fullVersion = ua.substring(verOffset+8);
}
// Microsoft IE
else if ((verOffset=ua.indexOf("MSIE"))!=-1) {
 browserName = "Microsoft Internet Explorer";
 fullVersion = ua.substring(verOffset+5);
}

//Apple Safari 
else if ((verOffset=ua.indexOf("Safari"))!=-1) {
 browserName = "Safari";
 fullVersion = ua.substring(verOffset+7);
 if ((verOffset=ua.indexOf("Version"))!=-1) 
   fullVersion = ua.substring(verOffset+8);
}
//Firefox 
else if ((verOffset=ua.indexOf("Firefox"))!=-1) {
 browserName = "Firefox";
 fullVersion = ua.substring(verOffset+8);
}
//other browsers 
else if ( (nameOffset=ua.lastIndexOf(' ')+1) < 
          (verOffset=ua.lastIndexOf('/')) ) 
{
 browserName = ua.substring(nameOffset,verOffset);
 fullVersion = ua.substring(verOffset+1);
 if (browserName.toLowerCase()==browserName.toUpperCase()) {
  browserName = navigator.appName;
 }
}
// Browser's fullVersion string including space/semicolon
if ((ix=fullVersion.indexOf(";"))!=-1)
   fullVersion=fullVersion.substring(0,ix);
if ((ix=fullVersion.indexOf(" "))!=-1)
   fullVersion=fullVersion.substring(0,ix);

//Get version Integer
majorVer = parseInt(''+fullVersion,10);
if (isNaN(majorVer)) {
 fullVersion  = ''+parseFloat(navigator.appVersion); 
 majorVer = parseInt(navigator.appVersion,10);
}

document.write(''
 +'Browser name  = '+browserName+'<br>'
 +'Full version  = '+fullVersion+'<br>'
 +'Major version = '+majorVer+'<br>'
 +'navigator.appName = '+navigator.appName+'<br>'
 +'navigator.userAgent = '+navigator.userAgent+'<br>'
)


Output of above code is:

Browser name = Chrome
Full version = 46.0.2490.71
Major version = 46
navigator.appName = Netscape
navigator.userAgent = Mozilla/5.0 (Macintosh; Intel Mac OS X 10_10_5) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/46.0.2490.71 Safari/537.36

Run above code here

Javascript detect browser

In above code Browser name will give name of the browser.

Javascript detect browser Version

Fullversion will return version of the browser including dot/colon.
Majorversion will return integer value.


2 comments:

  1. Hi, Really great effort. Everyone must read this article. Thanks for sharing.

    ReplyDelete
  2. What you're saying is completely true. I know that everybody must say the same thing, but I just think that you put it in a way that everyone can understand. I'm sure you'll reach so many people with what you've got to say.

    ReplyDelete

Powered by Blogger.