如何不使用window.navigator可靠地检测浏览器? [英] How can I reliably detect the browser without using window.navigator?

查看:42
本文介绍了如何不使用window.navigator可靠地检测浏览器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道Stack Overflow上有一千个有关使用JavaScript检测浏览器的问题.我的问题是,如何在没有 window.navigator (其中包括 navigator.userAgent )的情况下检测浏览器?

I know there are a thousand questions on Stack Overflow about detecting the browser with JavaScript. My question is how can you detect the browser without window.navigator (which includes navigator.userAgent)?

首先,需要澄清的是,我不需要了解渲染引擎,这不是用于自适应布局的,也不必惊慌:我已经在进行特征检测.如果为什么我要检测检测浏览器很重要,请发表评论,我很乐意加入解释,但这可能会使问题冗长.

First, to clarify, I don't need to know the rendering engine, this isn't for adaptive layout, and don't panic: I'm already doing feature detection. If why I'm asking about detecting the browser is important, please comment and I'll be happy to splice in the explanation, but it will probably make the question egregiously long.

接下来,让我描述为什么我的问题与以下内容不重复:

Next, let me describe why my question is not a duplicate of:

  • 使用Javascript进行浏览器检测? 是因为 19 个答案,其中 12 个专门使用 navigator.userAgent (包括使用userAgent的 jQuery.browser ),现在无论如何都消失了), 4 使用 navigator.appName (在Chrome中提供了"Netscape" ...), 1 避开了问题通过推荐与浏览器检测不同的功能检测(我已经在使用功能检测,但是要知道我可以使用它们的程度,我需要浏览器检测),而 2 并不是真正的答案或特定于IE.(尽管此非答案实际上非常说明了我在这里提出的问题为何与之相关:我正在努力避免陷入痛苦点在某些浏览器上会导致选项卡崩溃!)由于我的问题是在不使用 window.navigator 的情况下寻求答案(甚至是黑客?),因此它不是该问题的重复项.

  • Browser detection in Javascript? because of 19 answers, 12 of them use navigator.userAgent specifically (including jQuery.browser which used userAgent, and is now gone anyway), 4 use navigator.appName (which gives "Netscape" in Chrome...), 1 side-steps the question by recommending feature detection, which is different from browser detection (I am already using feature detection, but to know the extent to which I can use them, I need browser detection), and 2 aren't really answers or are IE-specific. (Although this non-answer is actually very explanatory about why my question here is relevant: I'm trying to avoid hitting pain points on certain browsers that would crash the tab!) Since my question is asking for an answer (even a hack?) without using window.navigator, it is not a duplicate of that question.

检查用户是否正在使用IE 因为(共11个答案),其中10个使用 navigator.userAgent ,其中1个使用IE技巧仅检测IE,但这不足以回答我的问题(尽管可能是这里发布的有用解决方案的一小部分)?

Check if the user is using IE because of 11 answers, 10 use navigator.userAgent and 1 of them uses an IE trick to detect IE only, which is not sufficient to answer my question (though it may be potentially be a small part of a helpful solution posted here)?

In Javascript, how do I determine if my current browser is Firefox on a computer vs everything else? because of 11 answers, 8 use navigator.userAgent, 2 recommend feature detection (again, not my question), and 1 isn't even an answer, really.

如何检测chrome和safari浏览器(webkit) 因为,其中有8个答案,其中6个使用 navigator.userAgent ,而2个是特定于Webkit的.不幸的是,WebKit不一定仅限于Safari,我需要了解浏览器,而不是渲染引擎.

How to detect chrome and safari browser (webkit) because of 8 answers, 6 of them use navigator.userAgent, and 2 are webkit-specific. Unfortunately, WebKit is not necessarily tied to just Safari, and I need to know the browser, not the rendering engine.

希望那是透明的.

我知道还有其他方法可以做到这一点,但是我不太了解每种浏览器的来龙去脉.可能在某些浏览器中是否存在一致或可靠地暴露给JavaScript的对象或变量?我知道有些实验性的API是供应商前缀的,但在商业产品中使用它似乎不是一个好主意,尽管我愿意在需要时将其降低到最低.还有其他可能性吗?

I know there are other ways to do this, but I don't know the ins-and-outs of each browser well enough. Are there objects or variables that are consistently or reliably exposed to JavaScript in certain browsers, maybe? I know that some experimental APIs are vendor-prefixed, but that doesn't seem like a good idea for use in a commercial product, although I'm willing to stoop that low if needed. Any other possibilities?

推荐答案

一个想法:

  • IE使用ActiveX(仍然可以使用IE11,但仍可以使用旧版),您可以通过查看ActiveX可用性来轻松推断用户使用IE的事实,但是,如果启用了安全设置,则需要依靠ActiveX,猜猜是什么,其他功能检测.
  • Chrome和Firefox都支持使用扩展程序,也许检测到这些扩展程序会有所帮助
  • Chrome浏览器的 window ['chrome'] ['webstore'] 对象在全局范围内可用.
  • 您可以使用 Object.keys 对窗口对象进行排序,并查找特定于供应商的名称,例如'moz''ms''o'.
  • IE uses ActiveX (still does up to IE11, legacy), You can fairly easy deduce the fact that the user is using IE from looking at activeX availability, however if the security settings are on, you need to fall back on, guess what, other feature detection.
  • Chrome and Firefox both support the use of extensions, maybe detecting these extensions will help
  • Chrome has the window['chrome']['webstore'] object available in the global scope.
  • You can sort through the window object with Object.keys and look for vendor specific names like 'moz' or 'ms' or 'o'.

如果将moz,ms和chrome-object结合使用,则可以嗅探出三种最大的浏览器.

If you combine moz, ms and chrome-object you can sniff out the three largest browsers.

顺便说一句,功能检测仍然是最好的选择,不是对OP而言,而是对我如何嗅探浏览器程序员" .

On a side note, feature detection is still the best option, not for the OP, but for the "I'm-just-getting-into-programming-and-I-like-to-know-how-I-sniff-out-a-browser-programmers" out there.

这篇关于如何不使用window.navigator可靠地检测浏览器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆