Fork me on GitHub

检测浏览器是否存在某个css或者js的api

利用能力检测检测是否存在某个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;
        }

  作为开发人员都是在一定能力范围内,浏览器的能力范围内搞开发的。能力检测可以检测浏览器是否具备这种能力。

 

posted @ 2021-05-06 14:33  我站在山顶上  阅读(162)  评论(0编辑  收藏  举报