Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Determine what Browser being used, using javascript

I have a problem in Determining Browsers.

I've tried using navigator and well, it did not help.

I used alert(navigator.appName); to determine the browser and I'm currently using Google Chrome, when the pop up appears it displayed Mozilla, but in Mozilla it works fine and with Mozilla It self.

is there a problem with the code? or it's some bug?

like image 963
Christian Eric Paran Avatar asked May 08 '12 20:05

Christian Eric Paran


2 Answers

navigator.sayswho= (function(){
    var N= navigator.appName, ua= navigator.userAgent, tem,
    M= ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*([\d\.]+)/i);
    if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
    M= M? [M[1], M[2]]:[N, navigator.appVersion, '-?'];
    return M.join(' ');
})();

alert(navigator.sayswho)
like image 185
kennebec Avatar answered Oct 30 '22 16:10

kennebec


It's close to chrome, if you need a simple short solution try to use this:

function getBrowser() {
  if( navigator.userAgent.indexOf("Chrome") != -1 ) {
    return "Chrome";
  } else if( navigator.userAgent.indexOf("Opera") != -1 ) {
    return "Opera";
  } else if( navigator.userAgent.indexOf("MSIE") != -1 ) {
    return "IE";
  } else if( navigator.userAgent.indexOf("Firefox") != -1 ) {
    return "Firefox";
  } else {
    return "unknown";
  }
}
like image 24
Alexander Blacks Avatar answered Oct 30 '22 17:10

Alexander Blacks