mishe / blog

前端碰上的问题或体会
230 stars 39 forks source link

浏览器检测 (转自个人百度空间) #34

Open mishe opened 8 years ago

mishe commented 8 years ago
(function(){
    var t={};
    t.language=navigator.language||navigator.userLanguage;
    t.cookie=navigator.cookieEnabled;
    t.java=navigator.javaEnabled();

    function getPlugin(pname){
        var plugins=navigator.plugins,
            len=plugins.length;
        if(plugins && len>0 ){
            for (var l = 0; l < len; l++) {
                if(plugins[l].name.indexOf(pname.name)>-1) return true
            }
            return false;
        }else{
            try{new ActiveXObject(pname.fullname);
                return true;
            }
            catch(e){
                return false
            }
        }
    }
    t.flash=getPlugin({name:'Flash',fullname:'ShockwaveFlash.ShockwaveFlash'});
    t.screen={
        ah:screen.availHeight,
        aw:screen.availWidth,
        cd:screen.colorDepth,
        h:screen.height,
        w:screen.width
    }
    function cookie(name, value, options){
        if (typeof value != 'undefined') {
            options = options || {};
            if (value === null) {
                value = '';
                options.expires = -1;
            }
            var expires = '', date;
            if (options.expires && (typeof options.expires == 'number' || options.expires.toUTCString)) {
                if (typeof options.expires == 'number') {
                    date = new Date();
                    date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
                } else {
                    date = options.expires;
                }
                expires = '; expires=' + date.toUTCString();
            }
            var path = '; path=' + (options.path ? options.path : '/');
            var domain = options.domain ? '; domain=' + options.domain : '';
            var secure = options.secure ? '; secure' : '';
            document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain, secure].join('');
        } else {
            var cookieValue = null;
            if (document.cookie && document.cookie != '') {
                var cookies = document.cookie.split(';');
                for (var i = 0; i < cookies.length; i++) {
                    var cookie = $.trim(cookies[i]);
                    if (cookie.substring(0, name.length + 1) == (name + '=')) {
                        cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
                        break;
                    }
                }
            }
            return cookieValue;
        }
    }

    var ua=navigator.userAgent;

    /* 操作系统监测 */
    function getSystem(){
        var ver, s,
            system=navigator.platform,
            win= system.indexOf('Win')== 0,
            mac= system.indexOf('Mac')== 0,
            x11= system.indexOf('X11')==0 || system.indexOf('Linux')== 0;
        if(win){
            if(/Win(?:dows\s)?([^do]{2}) (\d+\.\d+)/.test(ua)){
                if(RegExp.$1=='NT'){
                    switch(RegExp.$2){
                        case '5.0':ver='2000';break;
                        case '5.1':ver='XP';break;
                        case '6.0':ver='Vista';break;
                        case '6.1':ver='7';break;
                        default :ver='NT';break;
                    }
                }else if(RegExp.$1=='9x'){
                    ver='ME'
                }else{
                    ver=RegExp.$1
                }
            }

            s= 'Win '+ver;
        }else if(mac){
            s=system;
        }else if(x11){
            s=system
        }
        return s;
    }

    /* 移动监测 */
    function getMobile(){
        var m,f;

        if(/Nokia([\w\s]+)/.test(ua)){
            m='NOKIA '+RegExp.$1;
        }

        if(/Windows Phone (?:OS)?(\d+.\d+)/.test(ua)){
            m='WinPhone '+RegExp.$1;
            if(/NOKIA(?:;)? ([\w\s]+)/.test(ua)){
                m='NOKIA '+RegExp.$1;
            }
        }

        if(mac && ua.indexOf('Mobile')>-1){
            if(/(\w+); CPU (?:iPhone )?OS (\d+_\d+)/.test(ua)){
                m=RegExp.$1+' '+RegExp.$2.replace(/_/g,'.');
            }
        }

        if(/Android/.test(ua)){
            f=/([\w\s]+) Build/.test(ua);
            m=RegExp.$1.replace(' ','');

        }

        if(/BlackBerry([^/]*)/i.text(ua)){
            m='BlackBerry '+RegExp.$1
        }
        return m;
    }

    /* 浏览器及版本 */
    function getBrowser(){
        var b;
        if(window.opera){
            b='Opera '+window.opera.version();
        }else if(/AppleWebKit\/(\S+)/.test(ua)){
            b='Webkit '+RegExp.$1;
            if(/Chrome\/(\S+)/.test(ua)){
                b='Chrome '+RegExp.$1;
            }else if(/Version\/(\S+)/.test(ua)){
                b='Safari '+RegExp.$1;
            }
        }else if(/KHTML\/(\S+)/.test(ua) || /Konqueror\/([^;]+)/.test(ua)){
            b='Konqueror '+RegExp.$1;
        }else if(/rv:(\S+) Gecko\/\d{8}/.test(ua)){
            b='Gecko '+RegExp.$1;
            if(/Firefox\/(\S+)/.test(ua)){
                b='Firefox '+RegExp.$1
            }
        }else if(/MSIE ([^;]+)/.test(ua)){
            b='IE '+RegExp.$1;
        }
        return b;
    }

})