JavaScript determines browser (supports kernel, shell, version)

$(function() {
    alert(Browser.client.name+ " " +Browser.client.version + "  " + Browser.client.type);
});

var Browser=Browser || (function(window){
    var document = window.document,
        navigator = window.navigator,
        agent = navigator.userAgent.toLowerCase(),
         // IE8+ support. Returns the mode used by the browser to render the current document 
        // IE6,IE7:undefined.IE8:8(compatibility mode returns 7).IE9:9(compatibility mode returns 7 ||8) 
        // IE10:10 (compatibility mode 7||8||9) 
        IEMode = document.documentMode,
         // chorme 
        chrome = window.chrome || false ,
        System = {
             // user-agent 
            agent : agent,
             // Is it IE 
            isIE : /msie/ .test(agent),
             // Gecko kernel 
            isGecko: agent.indexOf( "gecko" )> 0 && agent.indexOf( " like gecko" )< 0 ,
             // webkit kernel 
            isWebkit: agent.indexOf( "webkit" )> 0 ,
             // whether it is standard mode 
            isStrict: document.compatMode === "CSS1Compat" ,
             // whether subtitle is supported 
            supportSubTitle:function(){
                return "track" in document.createElement( "track" );
            },
            //是否支持scoped
            supportScope:function(){
                return "scoped" in document.createElement( "style" );
            },
            // Get the version number of IE 
            ieVersion: function (){
                 try {
                     return agent.match(/msie ([\d.]+)/)[ 1 ] || 0 ;
                } catch (e) {
                    console.log( "error" );
                    return IEMode;
                }
            },
            // Opera version number 
            operaVersion: function (){
                 try {
                     if (window.opera) {
                         return agent.match(/opera.([\d.]+)/)[ 1 ];
                    } else if (agent.indexOf( "opr" ) > 0 ) {
                        return agent.match(/opr\/([\d.]+)/)[ 1 ];
                    }
                } catch (e) {
                    console.log( "error" );
                    return 0 ;
                }
            },
            // Description: version filtering. For example, 31.0.252.152 only keeps 31.0 
            versionFilter: function (){
                 if (arguments.length === 1 && typeof arguments[ 0 ] === "string" ) {
                     var version = arguments[ 0 ] ;
                    start = version.indexOf( "." );
                    if (start> 0 ){
                        end = version.indexOf( "." ,start+ 1 );
                        if (end !== - 1 ) {
                            return version.substr( 0 ,end);
                        }
                    }
                    return version;
                } else if (arguments.length === 1 ) {
                    return arguments[ 0 ];
                }
                return 0 ;
            }
        };

    try {
         // Browser type (IE, Opera, Chrome, Safari, Firefox) 
        System.type = System.isIE? "IE" :
            window.opera || (agent.indexOf( "opr" ) > 0 )? "Opera" :
                (agent.indexOf( "chrome" )> 0 )? "Chrome" :
                     // safari also provides a special judgment method 
                    window.openDatabase? "Safari" :
                        (agent.indexOf( "firefox" )> 0 )? "Firefox" :
                            'unknow' ;

        //版本号
        System.version = (System.type === "IE" )?System.ieVersion():
            (System.type === "Firefox" )?agent.match(/firefox\/([\d.]+)/)[ 1 ]:
                (System.type === "Chrome" )?agent.match(/chrome\/([\d.]+)/)[ 1 ]:
                    (System.type === "Opera" )?System.operaVersion():
                        (System.type === "Safari" )?agent.match(/version\/([\d.]+)/)[ 1 ]:
                            "0" ;

        // Browser shell 
        System.shell= function (){
             // Travel browser 
            if (agent.indexOf( "maxthon" ) > 0 ) {
                System.version = agent.match(/maxthon\/([\d.]+)/)[ 1 ] || System.version ;
                 return "Maxthon Browser" ;
            }
            // QQ browser 
            if (agent.indexOf( "qqbrowser" ) > 0 ) {
                System.version = agent.match(/qqbrowser\/([\d.]+)/)[ 1 ] || System.version ;
                return "QQ浏览器" ;
            }

            // Sogou browser 
            if ( agent.indexOf( "se 2.x" )> 0 ) {
                 return 'Sogou browser' ;
            }

            //Chrome:也可以使用window.chrome && window.chrome.webstore判断
            if (chrome && System.type !== "Opera" ) {
                var external = window.external,
                    clientInfo = window.clientInformation,
                     // client language: zh-cn, zh.360 will return undefined 
                    clientLanguage = clientInfo.languages;

                // Cheetah Browser: or agent.indexOf("lbbrowser")>0 
                if ( external && 'LiebaoGetVersion' in external) {
                     return 'Cheetah Browser' ;
                }
                // Baidu browser 
                if (agent.indexOf( "bidubrowser" )> 0 ) {
                    System.version = agent.match(/bidubrowser\/([\d.]+)/)[ 1 ] || 
                        agent.match( /chrome\/([\d.]+)/)[ 1 ];
                     return "Baidu Browser" ;
                }
                // 360 Fast Browser and 360 Safe Browser 
                if ( System.supportSubTitle() && typeof clientLanguage === "undefined" ) {
                     // object.key() returns an array. Contains enumerable properties and method names 
                    var storeKeyLen = Object.keys(chrome.webstore).length,
                        v8Locale = "v8Locale" in window;
                     return storeKeyLen > 1 ? '360 Fast Browser' : '360 Safe Browser' ;
                }
                return "Chrome" ;
            }
            return System.type;
        };

        // Browser name (if it is a shell browser, return the shell name) 
        System.name = System.shell();
         // Filter the version number 
        System.version = System.versionFilter(System.version);

    } catch (e) {
        console.log( "error" );
    }
    return {
        client:System
    };

})(window);

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324976997&siteId=291194637