Answer by Rob W http://stackoverflow.com/questions/9847580/how-to-detect-safari-chrome-ie-firefox-and-opera-browser/9851769#9851769
Googling for browser reliable detection often results in checking the User agent string. This method is not reliable, because it's trivial to spoof this value.
I've written a method to detect browsers by duck-typing.
Only use the browser detection method if it's truly necessary, such as showing browser-specific instructions to install an extension. Use feature detection when possible.
Demo: https://jsfiddle.net/311aLtkz/
// Opera 8.0+
var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Firefox 1.0+
var isFirefox = typeof InstallTrigger !== 'undefined';
// Safari 3.0+ "[object HTMLElementConstructor]"
var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || safari.pushNotification);
// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;
// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;
// Chrome 1+
var isChrome = !!window.chrome && !!window.chrome.webstore;
// Blink engine detection
var isBlink = (isChrome || isOpera) && !!window.CSS;
The previous method depended on properties of the rendering engine (-moz-box-sizing
and -webkit-transform
) to detect the browser. These prefixes will eventually be dropped, so to make detection even more robust, I switched to browser-specific characteristics:
- Internet Explorer: JScript's Conditional compilation (up until IE9) and
document.documentMode
. - Edge: In Trident and Edge browsers, Microsoft's implementation exposes the
StyleMedia
constructor. Excluding Trident leaves us with Edge. - Firefox: Firefox's API to install add-ons:
InstallTrigger
- Chrome: The global
chrome
object, containing several properties including a documentedchrome.webstore
object. - Safari: A unique naming pattern in its naming of constructors. This is the least durable method of all listed properties and guess what? In Safari 9.1.3 it was fixed. So we are checking against
SafariRemoteNotification
, which was introduced after version 7.1, to cover all Safaris from 3.0 and upwards. - Opera:
window.opera
has existed for years, but will be dropped when Opera replaces its engine with Blink + V8 (used by Chromium).- Update 1: Opera 15 has been released, its UA string looks like Chrome, but with the addition of "OPR". In this version the
chrome
object is defined (butchrome.webstore
isn't). Since Opera tries hard to clone Chrome, I use user agent sniffing for this purpose. - Update 2:
!!window.opr && opr.addons
can be used to detect Opera 20+ (evergreen).
- Update 1: Opera 15 has been released, its UA string looks like Chrome, but with the addition of "OPR". In this version the
- Blink:
CSS.supports()
was introduced in Blink once Google switched on Chrome 28. It's of course, the same Blink used in Opera.
- Firefox 0.8 - 44
- Chrome 1.0 - 48
- Opera 8.0 - 34
- Safari 3.0 - 10
- IE 6 - 11
- Edge - 20-25
Updated in November 2016 to include detection of Safari browsers from 9.1.3 and upwards
Newest edge dont have window.StyleMedia