首页 > 代码库 > 区分浏览器类型

区分浏览器类型

How to detect Safari, Chrome, IE, Firefox and Opera browser?

Demo: https://jsfiddle.net/311aLtkz/

 1 // Opera 8.0+
 2 var isOpera = (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(‘ OPR/‘) >= 0;
 3 
 4 // Firefox 1.0+
 5 var isFirefox = typeof InstallTrigger !== ‘undefined‘;
 6 
 7 // Safari 3.0+ "[object HTMLElementConstructor]" 
 8 var isSafari = /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window[‘safari‘] || safari.pushNotification);
 9 
10 // Internet Explorer 6-11
11 var isIE = /*@cc_on!@*/false || !!document.documentMode;
12 
13 // Edge 20+
14 var isEdge = !isIE && !!window.StyleMedia;
15 
16 // Chrome 1+
17 var isChrome = !!window.chrome && !!window.chrome.webstore;
18 
19 // Blink engine detection
20 var isBlink = (isChrome || isOpera) && !!window.CSS;

 

区分浏览器类型