js 判断不同系统的浏览器类型

来源:互联网 发布:mib browser 软件下载 编辑:程序博客网 时间:2024/05/17 09:05

function Env(){
 var ua=navigator.userAgent.toLowerCase();
 function check(r){
  return r.test(ua);
 }
 return {
   //判断环境,操作系统、浏览器、是否是https连接等
  DOC : document,
        isStrict : DOC.compatMode == "CSS1Compat" ,
        isOpera : check(/opera/) ,
        isChrome : check(/\bchrome\b/) ,
        isWebKit : check(/webkit/) ,
        isSafari : !check(/\bchrome\b/)&& check(/safari/) ,
        isSafari2 : !check(/\bchrome\b/)&& check(/safari/)&& check(/applewebkit\/4/), // unique to Safari 2
        isSafari3 : !check(/\bchrome\b/)&& check(/safari/)&& check(/version\/3/),
        isSafari4 : !check(/\bchrome\b/)&& check(/safari/)&& check(/version\/4/),
        isIE : !check(/opera/) && check(/msie/) ,
        isIE7 : !check(/opera/) && check(/msie/)&& check(/msie 7/) ,
        isIE8 : !check(/opera/) && check(/msie/)&& check(/msie 8/) ,
        isIE6 : !check(/opera/) && check(/msie/)&&!check(/msie 7/)&& !check(/msie 8/),
        isGecko : !check(/webkit/)&& check(/gecko/),
        isGecko2 : check(/webkit/)&& check(/rv:1\.8/),
        isGecko3 : check(/webkit/)&& check(/rv:1\.9/),
        isBorderBox : !check(/opera/) && check(/msie/)&& DOC.compatMode != "CSS1Compat",
        isWindows : check(/windows|win32/),
        isMac : check(/macintosh|mac os x/),
        isAir : check(/adobeair/),
        isLinux : check(/linux/),
        isSecure : /^https/i.test(window.location.protocol),
       /**
         * 是否为空,如果允许allowBlank=true,则当v=''时返回true
         */
        isEmpty : function(v, allowBlank){
            return v === null || v === undefined || ((this.isArray(v) && !v.length)) || (!allowBlank ? v === '' : false);
        },

        /**
         * 是否为数组类型
         */
        isArray : function(v){
            return toString.apply(v) === '[object Array]';
        },

        /**
         * 是否为日期类型
         */
        isDate : function(v){
            return toString.apply(v) === '[object Date]';
        },

        /**
         * 是否为Object类型
         */
        isObject : function(v){
            return !!v && Object.prototype.toString.call(v) === '[object Object]';
        },

        /**
         * 判断是否是函数
         */
        isFunction : function(v){
            return toString.apply(v) === '[object Function]';
        },

        /**
         * 判断是否为数字
         */
        isNumber : function(v){
            return typeof v === 'number' && isFinite(v);
        },

        /**
         * 判断字符串类型
         */
        isString : function(v){
            return typeof v === 'string';
        },

        /**
         * 判断布尔类型
         */
        isBoolean : function(v){
            return typeof v === 'boolean';
        },

        /**
         * 判断是否为dom元素
         */
        isElement : function(v) {
            return !!v && v.tagName;
        },

        /**
         * 判断是否已定义
         */
        isDefined : function(v){
            return typeof v !== 'undefined';
        }
 }

然后var env = env(); 用env. 来取的所需的类型。

原创粉丝点击