我一直在寻找可以检测访问网站的用户使用的是火狐3还是火狐4的代码。我所找到的只是检测浏览器类型而不是版本的代码。

如何检测这样的浏览器版本?


当前回答

navigator.sayswho= (function(){ var ua= navigator.userAgent, tem, M= ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; if(/trident/i.test(M[1])){ tem= /\brv[ :]+(\d+)/g.exec(ua) || []; return 'IE '+(tem[1] || ''); } if(M[1]=== 'Chrome'){ tem= ua.match(/\b(OPR|Edge)\/(\d+)/); if(tem!= null) return tem.slice(1).join(' ').replace('OPR', 'Opera'); } M= M[2]? [M[1], M[2]]: [navigator.appName, navigator.appVersion, '-?']; if((tem= ua.match(/version\/(\d+)/i))!= null) M.splice(1, 1, tem[1]); return M.join(' '); })(); console.log(navigator.sayswho); // outputs: `Chrome 62`

其他回答

看看navigator。userAgent - Firefox/xxx.xxx. xxx。XXX是在结尾指定的。

在纯Javascript中,您可以在导航器上执行RegExp匹配。使用userAgent查找Firefox版本:

var uMatch = navigator.userAgent.match(/Firefox\/(.*)$/),
    ffVersion;
if (uMatch && uMatch.length > 1) {
    ffVersion = uMatch[1];
}

如果不是Firefox浏览器,ffVersion将未定义。

参见工作示例→

这里有更好的兼容性@kennebec snippet; 将返回浏览器名称和版本(返回72而不是72.0.3626.96)。

在Safari, Chrome, Opera, Firefox, IE, Edge, UCBrowser上测试,也在移动设备上测试。

function browser() {
    var userAgent = navigator.userAgent,
        match = userAgent.match(/(opera|chrome|crios|safari|ucbrowser|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [],
        result = {},
        tem;

    if (/trident/i.test(match[1])) {
        tem = /\brv[ :]+(\d+)/g.exec(userAgent) || [];
        result.name = "Internet Explorer";
    } else if (match[1] === "Chrome") {
        tem = userAgent.match(/\b(OPR|Edge)\/(\d+)/);

        if (tem && tem[1]) {
            result.name = tem[0].indexOf("Edge") === 0 ? "Edge" : "Opera";
        }
    }
    if (!result.name) {
        tem = userAgent.match(/version\/(\d+)/i); // iOS support
        result.name = match[0].replace(/\/.*/, "");

        if (result.name.indexOf("MSIE") === 0) {
            result.name = "Internet Explorer";
        }
        if (userAgent.match("CriOS")) {
            result.name = "Chrome";
        }

    }
    if (tem && tem.length) {
        match[match.length - 1] = tem[tem.length - 1];
    }

    result.version = Number(match[match.length - 1]);

    return result;
}

我用它来获得实际浏览器版本的名称和编号(int):

function getInfoBrowser() { var ua = navigator.userAgent, tem, M = ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || []; if (/trident/i.test(M[1])) { tem = /\brv[ :]+(\d+)/g.exec(ua) || []; return { name: 'Explorer', version: parseInt((tem[1] || '')) }; } if (M[1] === 'Chrome') { tem = ua.match(/\b(OPR|Edge)\/(\d+)/); if (tem != null) { let app = tem.slice(1).toString().split(','); return { name: app[0].replace('OPR', 'Opera'), version: parseInt(app[1]) }; } } M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, '-?']; if ((tem = ua.match(/version\/(\d+)/i)) != null) M.splice(1, 1, tem[1]); return { name: M[0], version: parseInt(M[1]) }; } function getBrowser(){ let info = getInfoBrowser(); $("#i-name").html(info.name); $("#i-version").html(info.version); } <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <input type="button" onclick="getBrowser();" value="Get Info Browser"/> <hr/> Name: <span id="i-name"></span><br/> Version: <span id="i-version"></span>

这个磨合

铬;火狐浏览器;狩猎;Internet Explorer (>= 9);歌剧;边缘

给我。

我使用这段javascript代码的基础上,我可以在其他帖子中找到。

var browserHelper = function () { var self = {}; /// IE 6+ self.isIEBrowser = function () { return /*@cc_on!@*/false || !!document.documentMode; }; /// Opera 8.0+ self.isOperaBrowser = function () { return (!!window.opr && !!opr.addons) || !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0; }; /// Firefox 1.0+ self.isFirefoxBrowser = function () { return typeof InstallTrigger !== 'undefined'; }; /// Safari 3.0+ self.isSafariBrowser = function () { return /constructor/i.test(window.HTMLElement) || (function (p) { return p.toString() === "[object SafariRemoteNotification]"; })(!window['safari'] || (typeof safari !== 'undefined' && window['safari'].pushNotification)); }; /// Edge 20+ self.isEdgeBrowser = function () { return !self.isIEBrowser() && !!window.StyleMedia; }; /// Chrome 1 - 87 self.isChromeBrowser = function () { return (!!window.chrome && (!!window.chrome.webstore || !!window.chrome.runtime)) || (navigator.userAgent.indexOf("Chrome") > -1) && !self.isOperaBrowser(); }; /// Edge (based on chromium) self.isEdgeChromiumBrowser = function () { return self.isChromeBrowser() && (navigator.userAgent.indexOf("Edg") != -1); }; /// Blink self.isBlinkBasedOnBrowser = function () { return (self.isChromeBrowser() || self.isOperaBrowser()) && !!window.CSS; }; /// Returns the name of the navigator self.browserName = function () { if (self.isOperaBrowser()) return "Opera"; if (self.isEdgeBrowser()) return "Edge"; if (self.isEdgeChromiumBrowser()) return "Edge (based on chromium)"; if (self.isFirefoxBrowser()) return "Firefox"; if (self.isIEBrowser()) return "Internet Explorer"; if (self.isSafariBrowser()) return "Safari"; if (self.isChromeBrowser()) return "Chrome"; return "Unknown"; }; return self; }; var bName = document.getElementById('browserName'); bName.innerText = browserHelper().browserName(); #browserName { font-family: Arial, Verdana; font-size: 1.2rem; color: #ff8000; text-align: center; border: 2px solid #ff8000; border-radius: .5rem; padding: .5rem; max-width: 25%; margin: auto; } <div id="browserName"></div>