gpt4 book ai didi

javascript - 检测客户端浏览器及其版本号的可靠方法是什么?

转载 作者:行者123 更新时间:2023-11-30 17:39:24 25 4
gpt4 key购买 nike

检测某人的浏览器及其版本号的可靠方法是什么?据我所知,像 JavaScript 中的 navigator 对象之类的东西根本无法达到这个目的,我遇到的许多这些真正混杂在一起的解决方案也没有。我遇到过一两个当前可用的 JavaScript 代码片段,它们会告诉我是否有人在使用 Firefox、Chrome 等,但它们没有描述每个浏览器的版本号。如何找到它,如何可靠地完成它? (前端是几个 Flex 应用程序。)

编辑

这个问题已经链接到一个非常相似的问题,建议我应该能够只使用那里的答案。我不能;由于未知原因,它不起作用。在我之前的编辑中,我发布的第一个示例来自另一个问题。请删除此链接。谢谢。

编辑

我知道这个问题已被问过一百万次,但我遇到的答案(包括 Stack Overflow 上已接受的答案)要么不起作用,要么不够好,无法使用。这就是为什么我要强调“可靠”这个词。阻碍的一件事是,在很多代码片段中,您会看到出现“Netscape”而不是“Internet Explorer”等。

例如,“Internet Explorer 11”、“Firefox 26”等。或者至少是类似的东西。

以下是一些运行不佳的代码片段示例:

很受欢迎,但是当我尝试时它不会运行:

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+(\.\d+)?)/g.exec(ua) || [];
return 'IE '+(tem[1] || '');
}
M= M[2]? [M[1], M[2]]:[navigator.appName, navigator.appVersion, '-?'];
if((tem= ua.match(/version\/([\.\d]+)/i))!= null) M[2]= tem[1];
return M.join(' ');
})();

在 Firefox 上运行良好,但说 IE 是 Netscape,并且在描述版本时给出非常复杂的结果:

function get_browser(){
var N=navigator.appName, ua=navigator.userAgent, tem;
var M=ua.match(/(opera|chrome|safari|firefox|msie)\/?\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[0];
}
function get_browser_version(){
var N=navigator.appName, ua=navigator.userAgent, tem;
var M=ua.match(/(opera|chrome|safari|firefox|msie)\/?\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[1];
}

alert(get_browser())
alert(get_browser_version())

这几乎就是我所需要的;只要它不需要任何最新版本,那么我也只需要版本号:

var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
var isFirefox = typeof InstallTrigger !== 'undefined'; // Firefox 1.0+
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
// At least Safari 3+: "[object HTMLElementConstructor]"
var isChrome = !!window.chrome && !isOpera; // Chrome 1+
var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6

像这样返回一个字符串的东西:

Mozilla/5.0 (Windows NT 6.1; WOW64; Trident/7.0; rv:11.0) like Gecko

对于 IE11 或调用 Firefox 和 IE“Netscape”并不是我想要的。 JavaScript 中的 navigator 对象在这类事情上存在很多问题。

最佳答案

没有可靠的方法来执行browser sniffing .我最好的建议是看一下 WhichBrowser - 表现的力量 http://html5test.com/

Everybody lies — House M.D.

This is a extremely complicated and almost completely useless browser sniffing library. Useless because you shouldn't use browser sniffing. So stop right now and go read something about feature detecting instead. I'm serious. Go away. You'll thank me later.

But why almost completely useless and not completely useless?

Well, there is always an exception to the rule. There is one valid reason to do browser sniffing: to gather intelligence about which browsers are used on your website. My website is html5test.com and I wanted to know which score belongs to which browser. And to do that you need a browser sniffing library.

Why is it extremely complicated?

Because everybody lies. Seriously, there is not a single browser that is completely truthful. Almost all browsers say they are Netscape 5 and almost all WebKit browsers say they are based on Gecko. Even Internet Explorer 11 now no longer claims to be IE at all, but instead an unnamed browser that is like Gecko. And it gets worse. That is why it is complicated.

The main part of this library runs on the server and looks at the headers send by the browser, but it also collects various data from the browser itself. The first thing it looks at is the user-agent header, but there are many more headers that contain clues about the identity of the browser. Once the server finds the identity of the browser, it then looks at the data from the browser itself and check some additional characteristics and tries to determine if the headers where perhaps lying. It then gives you the result.

关于安装/要求的注意事项

How to install it

Place the files in a directory on your server. The server should be able to handle PHP and included is a .htaccess file that instructs the server to also use PHP to parse the detect.js file. This is required and if your server does not support .htaccess files you need to find a way to make your server do the same.

帮助您在 IIS 上运行的注释而不是 apache

Translate .htaccess Content to IIS web.config

如果您让它在 IIS 上运行,那么您可能想要发布一些关于如何实现它的进一步说明。

关于javascript - 检测客户端浏览器及其版本号的可靠方法是什么?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21391593/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com