这种检测其实就是主要根据window.navigator.userAgent的值来判断浏览器类型和版本。
具体的注释在文件中:
上面的检测方法有些时候显得有些多余,我们很多时候仅仅需要知道浏览器的类型,除了IE而外,也不需要关心过多的版本问题。在Prototype 1.6中有比较简单的方法,在Prototype中的代码如下:
var
Prototype
=
{
Version: ' 1.6.0.2 ' ,
Browser: {
IE: !! (window.attachEvent && ! window.opera),
Opera: !! window.opera,
WebKit: navigator.userAgent.indexOf( ' AppleWebKit/ ' ) > - 1 ,
Gecko: navigator.userAgent.indexOf( ' Gecko ' ) > - 1 && navigator.userAgent.indexOf( ' KHTML ' ) == - 1 ,
MobileSafari: !! navigator.userAgent.match( / Apple.*Mobile.*Safari / )
},
//
}
Version: ' 1.6.0.2 ' ,
Browser: {
IE: !! (window.attachEvent && ! window.opera),
Opera: !! window.opera,
WebKit: navigator.userAgent.indexOf( ' AppleWebKit/ ' ) > - 1 ,
Gecko: navigator.userAgent.indexOf( ' Gecko ' ) > - 1 && navigator.userAgent.indexOf( ' KHTML ' ) == - 1 ,
MobileSafari: !! navigator.userAgent.match( / Apple.*Mobile.*Safari / )
},
//
}
这种方法用“对象检测法”和userAgent结合来区别各种浏览器。”!!”的作用是把对象转变成Boolean类型,对象存在即为true,不存在即为false。相比之下,这种方法更为简洁。自己改了一下命名,以便和之前的完整版本兼容。