检测主流浏览器(IE,Opera,Chrome,Firefox, Safari的类型和版本),具体看代码,如果有问题,欢迎留言

var Browser = (function(window) {
    var agent = window.navigator.userAgent;
    var IE10 = 'IE10'; //IE10及以下
    var IE11 = 'IE11';
    var Edge = 'Edge';
    var Opera = 'Opera';
    var Chrome = 'Chrome';
    var Firefox = 'Firefox';
    var Safari = 'Safari';
    var System = {
        type: '',
        version: '',
        getIe10Version: function() {
            try {
                return agent.match(/MSIE ([\d.]+)/)[1] || '0';
            } catch (e) {
                console.log(e);
                return '0';
            }
        },
        operaVersion: function() {
            try {
                if (agent.indexOf('Opera') > -1) {
                    return agent.match(/Opera.([\d.]+)/)[1];
                } else {
                    return agent.match(/OPR\/([\d.]+)/)[1];
                }
            } catch (e) {
                console.log(e);
                return 0;
            }
        },
        //描述:version过滤.如31.0.252.152 只保留31.0
        versionFilter: function() {
            if (arguments.length === 1 && typeof arguments[0] === 'string') {
                var version = arguments[0];
                var start, end;
                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 {
        // 检测浏览器类型
        if (/MSIE/.test(agent)) {
            System.type = IE10;
        } else if (/rv:([\d.]+)\) like gecko/.test(agent.toLowerCase())) {
            System.type = IE11;
        } else if (agent.indexOf('Edge') > -1) {
            System.type = Edge;
        } else if (agent.indexOf('Opera') > -1 || agent.indexOf('OPR') > -1) {
            System.type = Opera;
        } else if (agent.indexOf('Chrome') > -1 && agent.indexOf('Safari') > -1) {
            System.type = Chrome;
        } else if (agent.indexOf('Safari') > -1 && agent.indexOf('Chrome') === -1) {
            System.type = Safari;
        } else if (agent.indexOf('Firefox') > -1) {
            System.type = Firefox;
        } else {
            System.type = 'unknow';
        }
        // 版本号
        switch (System.type) {
            case IE10:
                System.version = System.getIe10Version();
                break;
            case IE11:
                System.version = '11';
                break;
            case Edge:
                System.version = 'edge';
                break;
            case Firefox:
                System.version = agent.match(/Firefox\/([\d.]+)/)[1];
                break;
            case Chrome:
                System.version = agent.match(/Chrome\/([\d.]+)/)[1];
                break;
            case Opera:
                System.version = System.operaVersion();
                break;
            case Safari:
                System.version = agent.match(/Version\/([\d.]+)/)[1];
                break;
            default:
                System.version = '0';
        }
        System.version = System.versionFilter(System.version);
        console.log(System.type,System.version);
    } catch (e) {
        console.log(e);
    }
})(window);

LINLIN
4 声望0 粉丝

你猜


引用和评论

0 条评论