我有5个插件/扩展用于FF、Chrome、IE、Opera和Safari.
如何识别用户浏览器并重定向(单击安装按钮后)以下载相应的插件?
我有5个插件/扩展用于FF、Chrome、IE、Opera和Safari.
如何识别用户浏览器并重定向(单击安装按钮后)以下载相应的插件?
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.
仅在确实必要时使用浏览器检测方法,例如显示特定于浏览器的安装扩展说明.Use feature detection when possible.
演示:https://jsfiddle.net/6spj1059/
// 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'] || (typeof safari !== 'undefined' && window['safari'].pushNotification));
// Internet Explorer 6-11
var isIE = /*@cc_on!@*/false || !!document.documentMode;
// Edge 20+
var isEdge = !isIE && !!window.StyleMedia;
// Chrome 1 - 79
var isChrome = !!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime);
// Edge (based on chromium) detection
var isEdgeChromium = isChrome && (navigator.userAgent.indexOf("Edg") != -1);
// Blink engine detection
var isBlink = (isChrome || isOpera) && !!window.CSS;
var output = 'Detecting browsers by ducktyping:<hr>';
output += 'isFirefox: ' + isFirefox + '<br>';
output += 'isChrome: ' + isChrome + '<br>';
output += 'isSafari: ' + isSafari + '<br>';
output += 'isOpera: ' + isOpera + '<br>';
output += 'isIE: ' + isIE + '<br>';
output += 'isEdge: ' + isEdge + '<br>';
output += 'isEdgeChromium: ' + isEdgeChromium + '<br>';
output += 'isBlink: ' + isBlink + '<br>';
document.body.innerHTML = output;
previous method取决于渲染引擎(-moz-box-sizing
和-webkit-transform
)的属性来检测浏览器.这些前缀最终将被删除,因此为了使检测更加可靠,我切换到浏览器特定的特征:
document.documentMode
.StyleMedia
构造函数.除go 三叉戟,我们只剩下边缘.InstallTrigger
chrome
对象,包含多个属性,包括一个记录在案的chrome.webstore
对象.chrome.webstore
在最近的版本中已弃用且未定义SafariRemoteNotification
,以涵盖3.0及以上的所有Safaris.window.opera
已经存在多年了,但当Opera用Blink+V8(Chromium使用)取代引擎时,它就变成了will be dropped.chrome
对象(但chrome.webstore
没有).由于Opera试图克隆Chrome,为此我使用了用户代理嗅探.!!window.opr && opr.addons
可用于检测Opera 20+(常青树).CSS.supports()
was introduced in Blink.当然,这和歌剧中的 blink 是一样的.2016年11月更新,包括检测9.1.3及以上版本的Safari浏览器
更新于2018年8月,更新了对Chrome、Firefox IE和EDGE的最新成功测试.
2019年1月更新,以修复chrome检测(因为window.chrome.webstore遭到弃用),并包括最新的chrome成功测试.
2019年12月更新,添加了基于 chromium 检测的边缘(基于@Nimesh comments ).