0

This is not working when opening on IE11 browser, Safari OSX 10.11. What is wrong?

var browser = '';
var browserVersion = 0;

if (/Opera[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
    browser = 'Opera';
} else if (/MSIE (\d+\.\d+);/.test(navigator.userAgent)) {
    browser = 'MSIE';
} else if (/Navigator[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
    browser = 'Netscape';
} else if (/Chrome[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
    browser = 'Chrome';
} else if (/Safari[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
    browser = 'Safari';
    /Version[\/\s](\d+\.\d+)/.test(navigator.userAgent);
    browserVersion = new Number(RegExp.$1);
} else if (/Firefox[\/\s](\d+\.\d+)/.test(navigator.userAgent)) {
    browser = 'Firefox';
}
if(browserVersion === 0){
    browserVersion = parseFloat(new Number(RegExp.$1));
}
alert(browser + "*" + browserVersion);

Outputs: *0 only

  • Why do you want to do this? There are many, many browsers that the above will not identify. If it's just for interest, there are databases of user agent strings (e.g. [*user-agents.org*](http://www.user-agents.org), [*useragentstring.org*](http://www.useragentstring.com/pages/All/)). – RobG Dec 07 '15 at 20:52

2 Answers2

1

IE11 no longer reports as MSIE, according to this list of changes, it's intentional to avoid mis-detection.

What you can do if you really want to know it's IE is to detect the Trident/ string in the user agent if navigator.appName returns Netscape, something like (the untested);

How to detect IE11?

Here's a little more from Microsoft:

https://msdn.microsoft.com/en-us/library/ms537503(v=vs.85).aspx#TriToken

Trident/7.0 IE11

Trident/6.0 Internet Explorer 10

Trident/5.0 Internet Explorer 9

Trident/4.0 Internet Explorer 8

On the Safari Topic, look at their latest userAgent string, it's does not have ver.ver in it, so you're regex fails on this:

Safari 7.0.3 Mozilla/5.0 (Macintosh; Intel Mac OS X 10_9_3) AppleWebKit/537.75.14 (KHTML, like Gecko) Version/7.0.3 Safari/7046A194A

Community
  • 1
  • 1
spozun
  • 872
  • 1
  • 6
  • 14
0

If you need only browser name you can try using this function, this should work for latest IE and Safari:

var BrowserDetect = function() {
        var nav = window.navigator,
        ua = window.navigator.userAgent.toLowerCase();
        // Detect browsers (only the ones that have some kind of quirk we need to work around)
        if ((nav.appName.toLowerCase().indexOf("microsoft") != -1 || nav.appName.toLowerCase().match(/trident/gi) !== null))
            return "IE";
        if (ua.match(/chrome/gi) !== null)
            return "Chrome";
        if (ua.match(/firefox/gi) !== null)
            return "Firefox";
        if (ua.match(/safari/gi) !== null)
            return "Safari";
        if (ua.match(/webkit/gi) !== null)
            return "Webkit";
        if (ua.match(/gecko/gi) !== null)
            return "Gecko";
        if (ua.match(/opera/gi) !== null)
            return "Opera";
        // If any case miss we will return null
        return null
    };
Borys Kupar
  • 1,631
  • 11
  • 24