利用能力检测检测是否存在某个js的api
// 检测是否存在某个api function isHostMethod(object, property){ let t = typeof object[property] return t == 'function' || (!!(t=='object' && object[property])) || t == 'unknowns' } let xhr = new ActiveXObject('Microsoft.XMLHttp') // 比如检测xhr是否存在open isHostMethod(xhr,'open')
利用能力检测检测是否存在某个属性
function supportCss3(style) { // 分别是google Safari ie var prefix = ['webkit', 'Moz', 'ms'], i, humpString = [], htmlStyle = document.documentElement.style, _toHumb = function (string) { return string.replace(/-(w)/g, function ($0, $1) { return $1.toUpperCase(); }); }; for (i in prefix) { humpString.push(_toHumb(prefix[i] + '-' + style)); humpString.push(_toHumb(style)); } for (i in humpString) { if (humpString[i] in htmlStyle) { return true } } return false; }
作为开发人员都是在一定能力范围内,浏览器的能力范围内搞开发的。能力检测可以检测浏览器是否具备这种能力。