客户端检测
1、能力检测 基本概念 定义:最常用也最为人们广泛接受的客户端检测形式是能力检测(又称特性检测) 目标:不是识别特定的浏览器,而是识别浏览器的能力,,基本模式如下: if (object.propertyInQuestion){ //使用 object.propertyInQuestion } 举例: function getElement(id){ if (document.getElementById){ return document.getElementById(id); } else if (document.all){ return document.all[id]; } else { throw new Error("No way to retrieve element!"); } } 要理解能力检测,首先必须理解两个重要的概念: 先检测达成目的的最常用的特性 必须测试实际要用到的特性 function getWindowWidth(){ if (document.all){ //假设是 IE return document.documentElement.clientWidth; //错误的用法!!! } else { return window.innerWidth; } } 更可靠的能力检测 错误的能力检测: //不要这样做!这不是能力检测—