how to get browser name in javascript [Chrome, Firefox, Safari, Opera, Edge , MS IE]?

how to get browser name in javascript – in this example you can learn to detect Safari, Chrome, IE, Firefox and Opera browser. “Mozilla” is the application code name for both Chrome, Firefox, IE, Safari, and Opera.

how to get browser name in javascript

To detect user browser information we use the navigator.userAgent property. To establish the actual name of the user’s Web browser, you can use the navigator.appName and navigator.userAgent properties.

javascript get browser name

gets the type of browser

function detectBrowser() { 
    if((navigator.userAgent.indexOf("Opera") || navigator.userAgent.indexOf('OPR')) != -1 ) {
        return 'Opera';
    } else if(navigator.userAgent.indexOf("Chrome") != -1 ) {
        return 'Chrome';
    } else if(navigator.userAgent.indexOf("Safari") != -1) {
        return 'Safari';
    } else if(navigator.userAgent.indexOf("Firefox") != -1 ){
        return 'Firefox';
    } else if((navigator.userAgent.indexOf("MSIE") != -1 ) || (!!document.documentMode == true )) {
        return 'IE';//crap
    } else {
        return 'Unknown';
    }
} 

javascript get browser name

var nVer = navigator.appVersion;
var navigtr = navigator.userAgent;
var userBrowserNm  = navigator.appName;
var getFlDesc  = ''+parseFloat(navigator.appVersion); 
var userMejorVer = parseInt(navigator.appVersion,10);
var nameOffset,vrOfSet,ix;

// In Opera 15+, the true version is after "OPR/" 
if ((vrOfSet=navigtr.indexOf("OPR/"))!=-1) {
 userBrowserNm = "Opera";
 getFlDesc = navigtr.substring(vrOfSet+4);
}
// In older Opera, the true version is after "Opera" or after "Version"
else if ((vrOfSet=navigtr.indexOf("Opera"))!=-1) {
 userBrowserNm = "Opera";
 getFlDesc = navigtr.substring(vrOfSet+6);
 if ((vrOfSet=navigtr.indexOf("Version"))!=-1) 
   getFlDesc = navigtr.substring(vrOfSet+8);
}
// In MSIE, the true version is after "MSIE" in userAgent
else if ((vrOfSet=navigtr.indexOf("MSIE"))!=-1) {
 userBrowserNm = "Microsoft Internet Explorer";
 getFlDesc = navigtr.substring(vrOfSet+5);
}
// In Chrome, the true version is after "Chrome" 
else if ((vrOfSet=navigtr.indexOf("Chrome"))!=-1) {
 userBrowserNm = "Chrome";
 getFlDesc = navigtr.substring(vrOfSet+7);
}
// In Safari, the true version is after "Safari" or after "Version" 
else if ((vrOfSet=navigtr.indexOf("Safari"))!=-1) {
 userBrowserNm = "Safari";
 getFlDesc = navigtr.substring(vrOfSet+7);
 if ((vrOfSet=navigtr.indexOf("Version"))!=-1) 
   getFlDesc = navigtr.substring(vrOfSet+8);
}
// In Firefox, the true version is after "Firefox" 
else if ((vrOfSet=navigtr.indexOf("Firefox"))!=-1) {
 userBrowserNm = "Firefox";
 getFlDesc = navigtr.substring(vrOfSet+8);
}
// In most other browsers, "name/version" is at the end of userAgent 
else if ( (nameOffset=navigtr.lastIndexOf(' ')+1) < 
          (vrOfSet=navigtr.lastIndexOf('/')) ) 
{
 userBrowserNm = navigtr.substring(nameOffset,vrOfSet);
 getFlDesc = navigtr.substring(vrOfSet+1);
 if (userBrowserNm.toLowerCase()==userBrowserNm.toUpperCase()) {
  userBrowserNm = navigator.appName;
 }
}
// trim the getFlDesc string at semicolon/space if present
if ((ix=getFlDesc.indexOf(";"))!=-1)
   getFlDesc=getFlDesc.substring(0,ix);
if ((ix=getFlDesc.indexOf(" "))!=-1)
   getFlDesc=getFlDesc.substring(0,ix);

userMejorVer = parseInt(''+getFlDesc,10);
if (isNaN(userMejorVer)) {
 getFlDesc  = ''+parseFloat(navigator.appVersion); 
 userMejorVer = parseInt(navigator.appVersion,10);
}

document.write(''
 +'User Browser name  = '+userBrowserNm+'<br>'
 +'User Full version  = '+getFlDesc+'<br>'
 +'User Major version = '+userMejorVer+'<br>'
 +'User navigator.appName = '+navigator.appName+'<br>'
 +'User navigator.userAgent = '+navigator.userAgent+'<br>'
)

Get browser name in JavaScript

console.log(navigator.userAgent);

Don’t Miss : How To Detect Safari, Chrome, IE, Firefox And Opera Browser?

The userAgent property inside navigator object returns the value of the user-agent header sent by the browser to the server.

I hope you get an idea about how to get browser name in javascript.
I would like to have feedback on my infinityknow.com blog.
Your valuable feedback, question, or comments about this article are always welcome.
If you enjoyed and liked this post, don’t forget to share.