3

I have a simple question - is there a way to do some function, that detects if a user is using older browser version, that I can set myself?

Something that help me to do if statement like this:

if(chromeVersion <= 48 || firefoxVersion <= 52 || ieVersion <= 10 || ...)
{//do something}

I need this because of modern plugins that I'm using and they're not supported by older browsers and can't pre-fix them.

Have a nice day!

Honzis Novák
  • 269
  • 3
  • 15
  • 3
    Browser sniffing was a bad idea 10 years ago and it's not got any better. I'd suggest using feature detection if you want to know if your JS code will/will not work in a given browser – Rory McCrossan Jan 12 '18 at 12:07
  • @RoryMcCrossan it's not much about a javascript. It's about a canvas animations that uses javascript and strong-new css animations (values, ik) – Honzis Novák Jan 12 '18 at 12:10
  • You can still use feature detection for that too, plus the code is quite simple and much more robust than a version detector: https://stackoverflow.com/questions/2745432/best-way-to-detect-that-html5-canvas-is-not-supported – Rory McCrossan Jan 12 '18 at 12:17
  • That's also very handy, thanks !! :) – Honzis Novák Jan 12 '18 at 12:36

2 Answers2

3

Get chrome version :

function getChromeVersion () {     
    var raw = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);

    return raw ? parseInt(raw[2], 10) : false;
}

For multiple browser

navigator.sayswho= (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) || [];
        return 'IE '+(tem[1] || '');
    }
    if(M[1]=== 'Chrome'){
        tem= ua.match(/\b(OPR|Edge)\/(\d+)/);
        if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera');
    }
    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(' ');
})();

console.log(navigator.sayswho);
Nims Patel
  • 1,048
  • 9
  • 19
1

Try as follows

var x = navigator.userAgent.match(/(Opera|Chrome|Safari|Firefox|Msie|trident(?=\/))\/?\s*(\d+)/);

console.log(x)
if (x[1] == "Chrome") {

  alert("Chrome")

  if (x[2] == "63") {

    alert("Chrome version is 63")

  }



}
Nisal Edu
  • 7,237
  • 4
  • 28
  • 34