我一直在寻找能够让我检测访问网站的用户是否使用 Firefox 3 或 4 的代码。但我找到的只是用于检测浏览器类型而不是版本的代码。
如何检测这样一个浏览器的版本?
我一直在寻找能够让我检测访问网站的用户是否使用 Firefox 3 或 4 的代码。但我找到的只是用于检测浏览器类型而不是版本的代码。
如何检测这样一个浏览器的版本?
navigator.sayswho= (function(){
var ua= navigator.userAgent;
var tem;
var 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`
tem = ua.match(/\b(OPR|Edge)\/(\d+)/i); if (tem != null) return {name : tem[1] === 'OPR' ? 'Opera' : tem[1], version : tem[2]};
- Vincent KetelaarsMozilla/5.0(Linux; Android 5.0; Nexus 9 Build/LRX21R; wv)AppleWebKit/537.36(KHTML, like Gecko)Version/4.0 Chrome/45.0.2454.95 Safari/537.36 [FB_IAB/FB4A;FBAV/50.0.0.10.54;]
非常突出的用户代理,所以不能忽略它... - Willster这是对Kennebec答案的改进。
mbrowser=function(){
this.spec_string= navigator.userAgent;
this.name= this.get_name();
this.version= this.get_version();
};
mbrowser.prototype.get_name=function(){
var spec_string=this.spec_string;
var matches=spec_string.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
// Work with the matches.
matches=matches[2]? [matches[1], matches[2]]: [navigator.appName, navigator.appVersion, '-?'];
// Trident.
if(/trident/i.test(matches[1])){
var temp=/\brv[ :]+(\d+)/g.exec(spec_string) || [];
return 'IE';
}
// Chrome.
if(matches[1]==='Chrome'){
var temp=spec_string.match(/\bOPR|Edge\/(\d+)/)
if(temp!=null) {return 'Opera';}
}
if((temp=spec_string.match(/version\/(\d+)/i))!=null){
matches.splice(1,1,temp[1]);
}
var name=matches[0];
return name;
};
mbrowser.prototype.get_version=function(){
var spec_string=this.spec_string;
var matches=spec_string.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) || [];
// Work with the matches.
matches=matches[2]? [matches[1], matches[2]]: [navigator.appName, navigator.appVersion, '-?'];
// Trident.
if(/trident/i.test(matches[1])){
var temp=/\brv[ :]+(\d+)/g.exec(spec_string) || [];
var version=(temp[1]||'');
return version;
}
// Chrome.
if(matches[1]==='Chrome'){
var temp=spec_string.match(/\bOPR|Edge\/(\d+)/)
var version=temp[1];
if(temp!=null) {return version;}
}
if((temp=spec_string.match(/version\/(\d+)/i))!=null){
matches.splice(1,1,temp[1]);
}
var version=matches[1];
return version;
};
// m=module.
var browser=new mbrowser();
console.log(browser.name); // Chrome
console.log(browser.version); // 109
这段代码从spec_string(navigator.userAgent)中推断出浏览器的名称和版本号。但是各种不同的spec_string都有其相应的浏览器名称和版本号。而且我没法检查其中的一小部分。如果您能发布一个您知道结果的浏览器名称和版本号的spec_string,那将非常好。然后我可以相应地更新代码。
接下来我会逐步建立一个spec_string项目列表。
'spec_string1'=>[name,number],
'spec_string2'=>[name,number],
将来,每当对代码进行更改时,它都可以自动在所有已知的spec_string转换上进行测试。
我们可以一起完成这个任务。
以下是截至2019年5月处理浏览器检测的几个知名库。
var result = bowser.getParser(window.navigator.userAgent);
console.log(result);
document.write("You are using " + result.parsedResult.browser.name +
" v" + result.parsedResult.browser.version +
" on " + result.parsedResult.os.name);
<script src="https://unpkg.com/bowser@2.4.0/es5.js"></script>
*基于Chromium内核支持Edge
console.log(platform);
document.write("You are using " + platform.name +
" v" + platform.version +
" on " + platform.os);
<script src="https://cdnjs.cloudflare.com/ajax/libs/platform/1.3.5/platform.min.js"></script>
console.log($.browser)
document.write("You are using " + $.browser.name +
" v" + $.browser.versionNumber +
" on " + $.browser.platform);
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery-browser/0.1.0/jquery.browser.min.js"></script>
var result = detect.parse(navigator.userAgent);
console.log(result);
document.write("You are using " + result.browser.family +
" v" + result.browser.version +
" on " + result.os.family);
<script src="https://cdnjs.cloudflare.com/ajax/libs/Detect.js/2.2.2/detect.min.js"></script>
console.log(BrowserDetect)
document.write("You are using " + BrowserDetect.browser +
" v" + BrowserDetect.version +
" on " + BrowserDetect.OS);
<script src="https://kylemit.github.io/libraries/libraries/BrowserDetect.js"></script>
以下是将Kennebec(K)的答案与Hermann Ingjaldsson(H)的答案结合起来:
navigator.browserSpecs = (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 {name:'IE',version:(tem[1] || '')};
}
if(M[1]=== 'Chrome'){
tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
if(tem != null) return {name:tem[1].replace('OPR', 'Opera'),version:tem[2]};
}
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:M[1]};
})();
console.log(navigator.browserSpecs); //Object { name: "Firefox", version: "42" }
if (navigator.browserSpecs.name == 'Firefox') {
// Do something for Firefox.
if (navigator.browserSpecs.version > 42) {
// Do something for Firefox versions greater than 42.
}
}
else {
// Do something for all other browsers.
}
使用这个:http://www.quirksmode.org/js/detect.html
alert(BrowserDetect.browser); // will say "Firefox"
alert(BrowserDetect.version); // will say "3" or "4"
_browser = {};
function detectBrowser() {
var uagent = navigator.userAgent.toLowerCase(),
match = '';
_browser.chrome = /webkit/.test(uagent) && /chrome/.test(uagent) &&
!/edge/.test(uagent);
_browser.firefox = /mozilla/.test(uagent) && /firefox/.test(uagent);
_browser.msie = /msie/.test(uagent) || /trident/.test(uagent) ||
/edge/.test(uagent);
_browser.safari = /safari/.test(uagent) && /applewebkit/.test(uagent) &&
!/chrome/.test(uagent);
_browser.opr = /mozilla/.test(uagent) && /applewebkit/.test(uagent) &&
/chrome/.test(uagent) && /safari/.test(uagent) &&
/opr/.test(uagent);
_browser.version = '';
for (x in _browser) {
if (_browser[x]) {
match = uagent.match(
new RegExp("(" + (x === "msie" ? "msie|edge" : x) + ")( |\/)([0-9]+)")
);
if (match) {
_browser.version = match[3];
} else {
match = uagent.match(new RegExp("rv:([0-9]+)"));
_browser.version = match ? match[1] : "";
}
break;
}
}
_browser.opera = _browser.opr;
delete _browser.opr;
}
detectBrowser();
console.log(_browser)
要检查当前浏览器是否为Opera,您需要执行以下操作
if (_browser.opera) { // Opera specific code }
编辑:修复了格式,修复了IE11和Opera/Chrome的检测,将结果从result
改为browserResult
。现在_browser
键的顺序不重要了。更新了jsFiddle链接。
2015/08/11 编辑:添加了Internet Explorer 12(EDGE)的新测试用例,修复了一个小的正则表达式问题。更新了jsFiddle链接。
function BrowserCheck()
{
var N= navigator.appName, ua= navigator.userAgent, tem;
var M= ua.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i);
if(M && (tem= ua.match(/version\/([\.\d]+)/i))!= null) {M[2]=tem[1];}
M= M? [M[1], M[2]]: [N, navigator.appVersion,'-?'];
return M;
}
这将返回一个数组,第一个元素是浏览器名称,第二个元素是完整的版本号以字符串格式表示。
function get_browser() {
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:'IE',version:(tem[1]||'')};
}
if(M[1]==='Chrome'){
tem=ua.match(/\bEdg\/(\d+)/)
if(tem!=null) {return {name:'Edge(Chromium)', version:tem[1]};}
tem=ua.match(/\bOPR\/(\d+)/)
if(tem!=null) {return {name:'Opera', version:tem[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: M[1]
};
}
var browser=get_browser(); // browser.name = 'Edge(Chromium)'
// browser.version = '86'
console.log(browser);
在纯Javascript中,您可以对navigator.userAgent
进行RegExp匹配,以查找Firefox版本:
var uMatch = navigator.userAgent.match(/Firefox\/(.*)$/),
ffVersion;
if (uMatch && uMatch.length > 1) {
ffVersion = uMatch[1];
}
ffVersion
将为undefined
。
查看工作示例 →