0

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.

2
  • am using firefox and this happens to work quickly testing in console: navigator.userAgent.toLowerCase().split('/').pop() returns "34.0" I wouldn't use this in production for all browsers without testing Commented Dec 21, 2014 at 20:06
  • I don't get the downvote, but oh, well Commented Dec 21, 2014 at 21:31

2 Answers 2

2

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.

Sign up to request clarification or add additional context in comments.

2 Comments

thanks, I am already using feature detection, but want to eliminate support for specific versions of browsers and below. However this is very general, I am very specific concerning firefox only.
@scooterlord I posted another answer which will only detect firefox's version.
2

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)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.