能力检测
能力检测的目标不是识别特定的浏览器,而是识别浏览器的能力。
能力检测需要注意两点:
- 先检测达成目的的最常用的特性。因为先检测最常用的特性可以保证代码最优化,因为在多数情况下都可以避免测试多个条件
- 必须测试实际要用到的特性。
举例来说,检测某个属性是否存在并不能确定对象是否支持排序。看了下面的代码,得到的结论就是尽量使用typeof进行能力检测
function isSortable(object) { return !!object.sort;//这样做是不取的 这不是能力检测 只检测是否存在相应的方法 } //如果用上面的方法 那下面的demo不是... var result = isSortable({ sort: true }); //这样更好,因为实际检测了sort是否为函数 function isSortable(object) { return typeof object.sort == 'function'; }
下面的这个方法是相应来说检测能力比较保险的方式
/* *@desc:在浏览器环境下测试任何对象的某个特性是否存在 */ function isHostMethod(object,property) { var t = typeof object[property]; return t == 'function' || (!!(t == 'object' && object[property])) || t == 'unknown'; }
下面的这个demo是检测浏览器是否支持Netscape 风格的插件和是否具有DOM1 级规定的能力
//need to wait until onload so body is available window.onload = function(){ function getWindowWidth(){ if (window.innerWidth){ return window.innerWidth; } else if (document.documentElement.clientWidth){ return document.documentElement.clientWidth; } else if (document.body.clientWidth){ return document.body.clientWidth; } } //determine if the browser has Netscape-style plugins var hasNSPlugins = !!(navigator.plugins && navigator.plugins.length); //determine if the browser has basic DOM Level 1 capabilities var hasDOM1 = !!(document.getElementById && document.createElement && document.getElementsByTagName); alert("Window " + getWindowWidth()); alert("Supports Netscape plugins? " + hasNSPlugins); alert("Supports Core DOM 1? " + hasDOM1); }
怪癖检测
IE8及更早版本的怪癖:如果某个实例属性与[Enumerable]标识为false的某个原型属性同名,那个该实例属性将不会出现在for-in循环中
var hasDontEnumQuirk = function(){ var o = { toString : function(){} }; for (var prop in o){ if (prop == "toString"){ return false; } } return true; }(); alert(hasDontEnumQuirk);
Safari3怪癖:会枚举被隐藏的属性
var hasEnumShadowsQuirk = function(){ var o = { toString : function(){} }; var count = 0; for (var prop in o){ if (prop == "toString"){ count++; } } return (count > 1); }(); alert(hasEnumShadowsQuirk);
用户代码检测
用到的client.js文件如下:
var client = function(){ //rendering engines var engine = { ie: 0, gecko: 0, webkit: 0, khtml: 0, opera: 0, //complete version ver: null }; //browsers var browser = { //browsers ie: 0, firefox: 0, safari: 0, konq: 0, opera: 0, chrome: 0, //specific version ver: null }; //platform/device/OS var system = { win: false, mac: false, x11: false, //mobile devices iphone: false, ipod: false, ipad: false, ios: false, android: false, nokiaN: false, winMobile: false, //game systems wii: false, ps: false }; //detect rendering engines/browsers var ua = navigator.userAgent; if (window.opera){ engine.ver = browser.ver = window.opera.version(); engine.opera = browser.opera = parseFloat(engine.ver); } else if (/AppleWebKit/(S+)/.test(ua)){ engine.ver = RegExp["$1"]; engine.webkit = parseFloat(engine.ver); //figure out if it's Chrome or Safari if (/Chrome/(S+)/.test(ua)){ browser.ver = RegExp["$1"]; browser.chrome = parseFloat(browser.ver); } else if (/Version/(S+)/.test(ua)){ browser.ver = RegExp["$1"]; browser.safari = parseFloat(browser.ver); } else { //approximate version var safariVersion = 1; if (engine.webkit < 100){ safariVersion = 1; } else if (engine.webkit < 312){ safariVersion = 1.2; } else if (engine.webkit < 412){ safariVersion = 1.3; } else { safariVersion = 2; } browser.safari = browser.ver = safariVersion; } } else if (/KHTML/(S+)/.test(ua) || /Konqueror/([^;]+)/.test(ua)){ engine.ver = browser.ver = RegExp["$1"]; engine.khtml = browser.konq = parseFloat(engine.ver); } else if (/rv:([^)]+)) Gecko/d{8}/.test(ua)){ engine.ver = RegExp["$1"]; engine.gecko = parseFloat(engine.ver); //determine if it's Firefox if (/Firefox/(S+)/.test(ua)){ browser.ver = RegExp["$1"]; browser.firefox = parseFloat(browser.ver); } } else if (/MSIE ([^;]+)/.test(ua)){ engine.ver = browser.ver = RegExp["$1"]; engine.ie = browser.ie = parseFloat(engine.ver); } //detect browsers browser.ie = engine.ie; browser.opera = engine.opera; //detect platform var p = navigator.platform; system.win = p.indexOf("Win") == 0; system.mac = p.indexOf("Mac") == 0; system.x11 = (p == "X11") || (p.indexOf("Linux") == 0); //detect windows operating systems if (system.win){ if (/Win(?:dows )?([^do]{2})s?(d+.d+)?/.test(ua)){ if (RegExp["$1"] == "NT"){ switch(RegExp["$2"]){ case "5.0": system.win = "2000"; break; case "5.1": system.win = "XP"; break; case "6.0": system.win = "Vista"; break; case "6.1": system.win = "7"; break; default: system.win = "NT"; break; } } else if (RegExp["$1"] == "9x"){ system.win = "ME"; } else { system.win = RegExp["$1"]; } } } //mobile devices system.iphone = ua.indexOf("iPhone") > -1; system.ipod = ua.indexOf("iPod") > -1; system.ipad = ua.indexOf("iPad") > -1; system.nokiaN = ua.indexOf("NokiaN") > -1; //windows mobile if (system.win == "CE"){ system.winMobile = system.win; } else if (system.win == "Ph"){ if(/Windows Phone OS (d+.d+)/.test(ua)){; system.win = "Phone"; system.winMobile = parseFloat(RegExp["$1"]); } } //determine iOS version if (system.mac && ua.indexOf("Mobile") > -1){ if (/CPU (?:iPhone )?OS (d+_d+)/.test(ua)){ system.ios = parseFloat(RegExp.$1.replace("_", ".")); } else { system.ios = 2; //can't really detect - so guess } } //determine Android version if (/Android (d+.d+)/.test(ua)){ system.android = parseFloat(RegExp.$1); } //gaming systems system.wii = ua.indexOf("Wii") > -1; system.ps = /playstation/i.test(ua); //return it return { engine: engine, browser: browser, system: system }; }();
浏览器检测的demo HTML页面的js代码如下:
<!DOCTYPE html> <html> <head> <title>User-agent Detection Example</title> <script type="text/javascript" src="client.js"></script> </head> <body> <h2>Rendering Engines</h2> <ul> <li>client.engine.ie = <script>document.write(client.engine.ie);</script></li> <li>client.engine.webkit = <script>document.write(client.engine.webkit);</script></li> <li>client.engine.gecko = <script>document.write(client.engine.gecko);</script></li> <li>client.engine.khtml = <script>document.write(client.engine.khtml);</script></li> <li>client.engine.opera = <script>document.write(client.engine.opera);</script></li> <li>client.engine.ver = <script>document.write(client.engine.ver);</script></li> </ul> <h2>Browsers</h2> <ul> <li>client.browser.ie = <script>document.write(client.browser.ie);</script></li> <li>client.browser.safari = <script>document.write(client.browser.safari);</script></li> <li>client.browser.firefox = <script>document.write(client.browser.firefox);</script></li> <li>client.browser.konq = <script>document.write(client.browser.konq);</script></li> <li>client.browser.opera = <script>document.write(client.browser.opera);</script></li> <li>client.browser.chrome = <script>document.write(client.browser.chrome);</script></li> <li>client.browser.ver = <script>document.write(client.browser.ver);</script></li> </ul> <h2>System</h2> <ul> <li>client.system.win = <script>document.write(client.system.win);</script></li> <li>client.system.mac = <script>document.write(client.system.mac);</script></li> <li>client.system.x11 = <script>document.write(client.system.x11);</script></li> <li>client.system.iphone = <script>document.write(client.system.iphone);</script></li> <li>client.system.ipod = <script>document.write(client.system.ipod);</script></li> <li>client.system.ipad = <script>document.write(client.system.ipad);</script></li> <li>client.system.ios = <script>document.write(client.system.ios);</script></li> <li>client.system.android = <script>document.write(client.system.android);</script></li> <li>client.system.nokiaN = <script>document.write(client.system.nokiaN);</script></li> <li>client.system.winMobile = <script>document.write(client.system.winMobile);</script></li> <li>client.system.wii = <script>document.write(client.system.wii);</script></li> <li>client.system.ps = <script>document.write(client.system.ps);</script></li> </ul> <center> </center> </body> </html>
总结一下: