0
点赞
收藏
分享

微信扫一扫

浏览器中环境判断和系统判断-js封装

浏览器中环境判断和系统判断-js封装_firefox

获取操作系统信息 

// 获取操作系统信息  
    function getOsInfo (){  
    var userAgent = navigator.userAgent.toLowerCase();  
    var name = 'Unknown';  
    var version = '';  
    if (userAgent.indexOf('win') > -1) {  
        name = 'Windows';  
		if (userAgent.indexOf('windows nt 5.0') > -1) {  
			version = '2000';  
		} else if (userAgent.indexOf('windows nt 5.1') > -1 || userAgent.indexOf('windows nt 5.2') > -1) {  
			version = 'XP';  
		} else if (userAgent.indexOf('windows nt 6.0') > -1) {  
			version = 'Vista';  
		} else if (userAgent.indexOf('windows nt 6.1') > -1 || userAgent.indexOf('windows 7') > -1) {  
			version = '7';  
		} else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows 8') > -1) {  
			version = '8';  
		} else if (userAgent.indexOf('windows nt 6.3') > -1) {  
			version = '8.1';  
		} else if (userAgent.indexOf('windows nt 6.2') > -1 || userAgent.indexOf('windows nt 10.0') > -1) {  
			version = '10';
			try {
                if(navigator.userAgentData){
                    navigator.userAgentData.getHighEntropyValues(["platformVersion"]).then(ua => {
                        if (navigator.userAgentData.platform === "Windows") {
                            if (parseInt(ua.platformVersion.split('.')[0]) >= 13) {
                                version = '11';
                            }
                        }
                    });
                }
            } catch (e) {
            }
		} else {  
			version = '';  
		}  
    } else if (userAgent.indexOf('iphone') > -1) {  
        name = 'iPhone';  
    } else if (userAgent.indexOf('mac') > -1) {  
        name = 'Mac';  
    } else if (userAgent.indexOf('x11') > -1 || userAgent.indexOf('unix') > -1 || userAgent.indexOf('sunname') > -1 || userAgent.indexOf('bsd') > -1) {  
        name = 'Unix';  
    } else if (userAgent.indexOf('linux') > -1) {  
        if (userAgent.indexOf('android') > -1) {  
        name = 'Android';  
    } else {  
        name = 'Linux';  
    }  
    } else {  
        name = 'Unknown';  
    }  
    return { name, version };  
    }

网络状态

function isWifi(){
        var wifi = 1;
        var ua = window.navigator.userAgent;
        try {
            var con = window.navigator.connection;
            if(con){
                var network = con.type|| con.effectiveType;
                if(network != "wifi" && network != "2" && network != "unknown"){
                    // unknown是为了兼容Chrome Canary
                    wifi = '0';
                }
            }
        } catch (e) {
        }
        return wifi;
    }




    function getNetworkType() {
        var network = 'Unknown';
        try {
            var con = window.navigator.connection;
            if(con) {
            network = con.type|| con.effectiveType;
            }
            if(network == "2" || network == "unknown"){
                // unknown是为了兼容Chrome Canary
                network = "wifi";
            }
        } catch (e) {
            return network;
        }
        return network;
    }

function getBrowser() {
        var Sys = {};  
        var ua = navigator.userAgent.toLowerCase();  
        var s;
        (s = ua.match(/rv:([\d.]+)\) like gecko/)) ? Sys.ie = s[1] :
        (s = ua.match(/msie ([\d\.]+)/)) ? Sys.ie = s[1] :  
        (s = ua.match(/edge\/([\d\.]+)/)) ? Sys.edge = s[1] :
        (s = ua.match(/edg\/([\d\.]+)/)) ? Sys.edg = s[1] :
        (s = ua.match(/firefox\/([\d\.]+)/)) ? Sys.firefox = s[1] :  
        (s = ua.match(/(?:opera|opr).([\d\.]+)/)) ? Sys.opera = s[1] :  
        (s = ua.match(/chrome\/([\d\.]+)/)) ? Sys.chrome = s[1] :  
		(s = ua.match(/version\/([\d\.]+).*safari/)) ? Sys.safari = s[1] : 0;  
		
		//QQ浏览器
		if (ua.indexOf("qqbrowser") > -1) return "QQ";

		//搜狗浏览器
		if (ua.indexOf("se 2.x") > -1) return 'sogou';

		//搜狗浏览器
		if (ua.indexOf("baidubrowser") > -1) return 'baidu';

		//uc浏览器
		if (ua.indexOf("ucbrowser") > -1) return 'uc';

        if (Sys.ie) return ('IE: ' + Sys.ie);  
        if (Sys.edge) return ('EDGE: ' + Sys.edge);
        if (Sys.edg) return ('EDGE: ' + Sys.edg);
        if (Sys.firefox) return ('Firefox: ' + Sys.firefox);  
        if (Sys.chrome) return ('Chrome: ' + Sys.chrome);  
        if (Sys.opera) return ('Opera: ' + Sys.opera);  
        if (Sys.safari) return ('Safari: ' + Sys.safari);
        return 'Unknown';
    }

function getFlashVersion(){
        var flashVersion = ''; //flash版本
        if (navigator.plugins && navigator.plugins.length > 0) {
            var swf = navigator.plugins["Shockwave Flash"];
            try {
                if (swf) {
                    var words = swf.description.split(" ");
                    for (var i = 0; i < words.length; ++i) {
                        if (isNaN(parseInt(words[i]))) continue;
                        flashVersion = parseInt(words[i]);
                    }
                }
            } catch (e) {
            }
        }
        return flashVersion;
    }

举报

相关推荐

0 条评论