JS 检测判断浏览器的方法

function getBrowser(elements) {
        var userAgent = navigator.userAgent;
    var regexps = {
                'Chrome': [ /Chrome\/(\S+)/ ],
                'Firefox': [ /Firefox\/(\S+)/ ],
                'MSIE': [ /MSIE (\S+);/ ],
                'Opera': [ /Opera\/.*?Version\/(\S+)/,          /* Opera 10 */
                                   /Opera\/(\S+)/  ],           /* Opera 9 and older */
                'Safari': [ /Version\/(\S+).*?Safari\// ]
        },
        re, m, browser, version;

    if (elements === undefined)
        {
        elements = 2;
        }
    else if (elements === 0)
        {
        elements = 1337;
        }

    for (browser in regexps)
        {
        while (re = regexps[browser].shift())
                {
            if (m = userAgent.match(re)) 
                        {
                version = (m[1].match(new RegExp('[^.]+(?:\.[^.]+){0,' + --elements + '}')))[0];
                                browserObj = { name : browser, version : version }
                                return browserObj;
            }
                }
        }
        return null;
}
除非注明, 风波竹林 文章均为原创,转载请以链接形式标明本文地址
本文地址: https://blog.fbzl.org/js检测判断浏览器的方法/