Prototype 提供了一些标志,您可以检查以了解正在运行的浏览器。请记住,检查您希望使用的功能比检查特定浏览器要好得多。
这是prototype.js
当前在源代码树中的浏览器和功能检测部分:
var Prototype = {
Browser: {
IE: !!(window.attachEvent &&
navigator.userAgent.indexOf('Opera') === -1),
Opera: navigator.userAgent.indexOf('Opera') > -1,
WebKit: navigator.userAgent.indexOf('AppleWebKit/') > -1,
Gecko: navigator.userAgent.indexOf('Gecko') > -1 &&
navigator.userAgent.indexOf('KHTML') === -1,
MobileSafari: !!navigator.userAgent.match(/Apple.*Mobile.*Safari/)
},
BrowserFeatures: {
XPath: !!document.evaluate,
SelectorsAPI: !!document.querySelector,
ElementExtensions: !!window.HTMLElement,
SpecificElementExtensions:
document.createElement('div')['__proto__'] &&
document.createElement('div')['__proto__'] !==
document.createElement('form')['__proto__']
},
}
因此,您可以通过调查 的值来检查当前浏览器是否是 IE Prototype.Browser.IE
,或者,可以更加未来兼容并检查特定功能,例如带有Prototype.BrowserFeatures.XPath
.