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
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);
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
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
};
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.