scooterlord
scooterlord

Reputation: 15369

Detecting specific browser versions using jQuery

I have this piece of code that successfully detects Mozilla Firefox:

function isFFold() {
  return (
    (navigator.userAgent.toLowerCase().indexOf("firefox") >= 0)
  );
}

How can I alter the code so that I can get specific version? I have looked everywhere but the only way I find is using the deprecated browser function.

Upvotes: 0

Views: 177

Answers (2)

maja
maja

Reputation: 18074

As you only want to know the version of Mozilla Firefox, you can also use this function:

function getFFversion(){
  var ua= navigator.userAgent, tem;
  var match = ua.match(/firefox\/?\s*(\d+)/i);    
  if(!match){     //not firefox
    return null;
  }
  if((tem= ua.match(/version\/(\d+)/i)) != null) {
    return parseInt(tem[1]);
  }
  return parseInt(match[1]);
}

It will return the version as an integer. If the browser is not Mozilla Firefox, it will return null.

Your test function can look as follows:

void isFFold(minVer){
  var version = getFFversion();
  return ( version!==null && version < minVer );
}

(It will return false for non-firefox browsers)

Upvotes: 2

maja
maja

Reputation: 18074

I always use this code to check the user agent and it's version:

navigator.sayWho= (function(){
    var ua= navigator.userAgent, tem,
    M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
    if(/trident/i.test(M[1])){
        tem=  /\brv[ :]+(\d+)/g.exec(ua) || [];
        navigator.isIE = true;
        return 'IE '+(tem[1] || '');
    }
    navigator.isIE = false;
    if(M[1]=== 'Chrome'){
        tem= ua.match(/\bOPR\/(\d+)/)
        if(tem!= null) return 'Opera '+tem[1];
    }
    M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?'];
    if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]);
    return M.join(' ');
})();

It will tell you the name and the version of any user agent. Unfortunately, I don't know the original source of this code anymore.

Note that this code also works without jQuery.

However, you should try to avoid such code and use feature detection instead.

Upvotes: 2

Related Questions