JavaScript 中的浏览器检测?
如何使用 JavaScript 确定确切的浏览器和版本?
How do I determine the exact browser and version using JavaScript?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
如何使用 JavaScript 确定确切的浏览器和版本?
How do I determine the exact browser and version using JavaScript?
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
接受
或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
发布评论
评论(25)
你可以阅读更多
如何检测 Safari、Chrome、IE、Firefox 和 Opera浏览器?
you can more read
How to detect Safari, Chrome, IE, Firefox and Opera browser?
我知道我回答这个问题已经太晚了,但我想我应该把我的片段放在这里。这里的很多答案都很好,而且,正如有人指出的那样,通常最好使用
功能检测
,而不是依赖userAgent
字符串。然而,如果您打算走这条路,我已经编写了一个完整的代码片段,以及一个替代的 jQuery 实现来替换已废弃的$.browser
。Vanilla JS
我的第一个代码片段只是向
navigator
对象添加了四个属性:browser
、version
、mobile
和 & 。webkit
。jsFiddle
I know I'm WAY late to this question, but figured I'd throw my snippets up here. A lot of the answers here are OK, and, as one points out, it's generally best to use
feature detection
rather than rely on theuserAgent
string. However, if you are going to go that route, I've written a complete snippet, as well as an alternate jQuery implementation to replace the depricated$.browser
.Vanilla JS
My first snippet simply adds four properties to the
navigator
object:browser
,version
,mobile
, &webkit
.jsFiddle
不完全是您想要的,但接近它:
变量将包含适当的版本,如果不可用,则为
false
。如果使用 Chrome 的人能够了解您是否可以以与
window.opera
类似的方式使用window.chrome
,我将不胜感激。Not exactly what you want, but close to it:
The variables will contain the appropriate version or
false
if it is not available.I'd appreciate it if someone using Chrome could find out if you can use
window.chrome
in a similar way towindow.opera
.有时我们需要简单的方法来检查浏览器是否是IE。它可能是这样的:
或简化的 siva 方法:
MSIE v.11 检查:
其他 IE 浏览器在其 userAgent 属性中包含 MSIE 字符串,并且可以被它捕获。
Some times we need simple method to check if the browser is IE or not. This is how it could be:
or simplified siva's method:
MSIE v.11 check:
other IE browsers contain MSIE string in their userAgent property and could be catched by it.
我发现了一些有趣且更快的方法。
IE 支持
navigator.systemLanguage
,它返回“en-US”,而其他浏览器返回undefined
。I found something interesting and quicker way.
IE supports
navigator.systemLanguage
which returns "en-US" where other browsers returnundefined
.我做了这个小功能,希望对你有帮助。在这里您可以找到最新版本
浏览器检测
I make this small function, hope it helps. Here you can find the latest version
browserDetection
下面的代码片段将展示如何根据 IE 版本和浏览器显示 UI 元素
下面的代码将给出如何获取 IE 版本
Below code snippet will show how how you can show UI elemnts depends on IE version and browser
Below code will give how we can get IE version
顾名思义,这将告诉您浏览器提供的名称和版本号。
当您在多个浏览器上测试新代码时,它可以方便地对测试和错误结果进行排序。
As the name implies, this will tell you the name and version number supplied by the browser.
It is handy for sorting test and error results, when you are testing new code on multiple browsers.
我建议使用小型 javascript 库 Bowser。它基于
navigator.userAgent
并针对所有浏览器(包括 iPhone、Android 等)进行了良好的测试。https://github.com/ded/bowser
你可以简单地说:
I recommend using the tiny javascript library Bowser. It is based on the
navigator.userAgent
and quite well tested for all browsers including iphone, android etc.https://github.com/ded/bowser
You can use simply say:
这是我为了获取客户信息而写的东西
This is something I wrote to get client info
以下是 2016 年检测浏览器的方法,包括 Microsoft Edge、Safari 10 和 Blink 检测:
这种方法的优点在于它依赖于浏览器引擎属性,因此它甚至涵盖了几乎兼容的衍生浏览器,例如 Yandex 或 Vivaldi与他们使用其引擎的主要浏览器。 Opera 是个例外,它依赖于用户代理嗅探,但如今(即版本 15 及更高版本)甚至 Opera 本身也只是 Blink 的一个外壳。
Here's how to detect browsers in 2016, including Microsoft Edge, Safari 10 and detection of Blink:
The beauty of this approach is that it relies on browser engine properties, so it covers even derivative browsers, such as Yandex or Vivaldi, which are practically compatible with the major browsers whose engines they use. The exception is Opera, which relies on user agent sniffing, but today (i.e. ver. 15 and up) even Opera is itself only a shell for Blink.
通常最好尽可能避免特定于浏览器的代码。 JQuery
$.support
属性可用于检测对特定功能的支持,而不是依赖于浏览器名称和版本。例如,在 Opera 中,您可以伪造 Internet Explorer 或 Firefox 实例。
详细说明JQuery.support 可以在这里找到: http://api.jquery.com/jQuery.support/< /a>
现在根据 jQuery 已弃用。
在编写网站代码时,我始终确保非 js 用户也可以访问导航等基本功能。如果主页针对特殊受众,这可能会引起讨论,并且可以忽略。
It is usually best to avoid browser-specific code where possible. The JQuery
$.support
property is available for detection of support for particular features rather than relying on browser name and version.In Opera for example, you can fake an internet explorer or firefox instance.
A detailed description of JQuery.support can be found here: http://api.jquery.com/jQuery.support/
Now deprecated according to jQuery.
When coding websites, I always make sure, that basic functionality like navigation is also accessible to non-js users. This may be object to discussion and can be ignored if the homepage is targeted to a special audience.
这会告诉您有关浏览器及其版本的所有详细信息。
This tells you all the details about your browser and the version of it.
有关 Web 浏览器的所有信息都包含在 navigator 对象中。名称和版本都在那里。
来源:javascript 浏览器检测
All the information about web browser is contained in navigator object. The name and version are there.
Source: javascript browser detection
由于 Internet Explorer 11 (IE11+) 出现并且不再使用
MSIE
标签名称,我提出了旧检测函数的变体:Since Internet Explorer 11 (IE11+) came out and is not using the tag name of
MSIE
anymore I came up with a variance of an older detection function:遗憾的是,IE11 的
navigator.userAgent
中不再有MSIE
:至于为什么你想知道你正在使用哪个浏览器,那是因为每个浏览器都有自己的一套错误,您最终会实施特定于浏览器和版本的解决方法,或者告诉用户使用不同的浏览器!
Sadly, IE11 no longer has
MSIE
in itsnavigator.userAgent
:As to why you want to know which browser you're using, it's because every browser has its own set of bugs, and you end up implementing browser and version specific workarounds, or tell the user to use a different browser!
但请注意,两者不一定反映事实。许多浏览器可以设置为与其他浏览器一样屏蔽。因此,举例来说,您无法始终确定用户是否真的使用 IE6 或冒充 IE6 的 Opera 进行冲浪。
Note, however, that both will not necessarily reflect the truth. Many browsers can be set to mask as other browsers. So, for example, you can't always be sure if a user is actually surfing with IE6 or with Opera that pretends to be IE6.
这个小库可能会对您有所帮助。但请注意,浏览器检测并不总是解决方案。
This little library may help you. But be aware that browser detection is not always the solution.
以下是我为 Internet Explorer 自定义 CSS 的方法:
在我的 JavaScript 文件中:
然后在我的 CSS 文件中,定义每种不同的样式:
Here is how I do custom CSS for Internet Explorer:
In my JavaScript file:
And then in my CSS file, y define each different style:
您可以扫描用户代理来查找浏览器名称,而不是对 Web 浏览器进行硬编码:
我已经在 Safari、Chrome 和 Firefox 上对此进行了测试。如果您发现这在浏览器上不起作用,请告诉我。
"Safari"
"Chrome"
"Firefox"
如果需要,您甚至可以修改此设置以获取浏览器版本。请注意,有更好的方法来获取浏览器版本
示例输出:
Instead of hardcoding web browsers, you can scan the user agent to find the browser name:
I've tested this on Safari, Chrome, and Firefox. Let me know if you found this doesn't work on a browser.
"Safari"
"Chrome"
"Firefox"
You can even modify this to get the browser version if you want. Do note there are better ways to get the browser version
Sample output:
如果您想要一个返回浏览器和版本的函数,这里是对原始答案的改进:
有了这个,它还在本地存储结果,因此不需要每次都运行此检查。
If you want a function that returns the browser as well as the version, here is an improvement from the original answer:
With this, it also stores the result locally, so this check is not necessary to run every time.
这就是我正在使用的:
info
属性:browser
:gc
for Google Chrome;ie9
-ie11
用于 IE;ie?
表示旧的或未知的 IE; Edge 的ed
;ff
适用于 Firefox;sa
用于 Safari;用于 Opera 的op
。操作系统
:mac
win7
win8
win10
winnt
< code>winxpwinvista
linux
nix
mobile
:a
适用于 Android ;i
适用于 iOS (iPhone iPad);w
适用于 Windows Phone;b
代表黑莓;s
用于运行 Safari 的未检测到的移动设备;1
用于其他未检测到的手机;0
对于非移动touch
:true
对于支持触摸的设备,包括同时具有鼠标和触摸功能的触摸笔记本电脑/笔记本电脑;false
表示不支持触摸tablet
:true
或false
https://jsfiddle.net/oriadam/ncb4n882/
This is what I'm using:
info
properties:browser
:gc
for Google Chrome;ie9
-ie11
for IE;ie?
for old or unknown IE;ed
for Edge;ff
for Firefox;sa
for Safari;op
for Opera.os
:mac
win7
win8
win10
winnt
winxp
winvista
linux
nix
mobile
:a
for Android;i
for iOS (iPhone iPad);w
for Windows Phone;b
for Blackberry;s
for undetected mobile running Safari;1
for other undetected mobile;0
for non-mobiletouch
:true
for touch enabled devices, including touch laptops/notebooks that has both mouse and touch together;false
for no touch supporttablet
:true
orfalse
https://jsfiddle.net/oriadam/ncb4n882/
您可以使用 jQuery 库来检测浏览器版本。
示例:
jQuery.browser.version
但是,只有当您还使用 jQuery 的其他功能时,这才有意义。添加整个库只是为了检测浏览器对我来说似乎有点矫枉过正。
更多信息:
http://api.jquery.com/jQuery.browser/
(您必须滚动下降一点)
You could use the jQuery library to detect the browser version.
Example:
jQuery.browser.version
However, this only makes sense if you are also using other functions of jQuery. Adding an entire library just to detect the browser seems like overkill to me.
More information:
http://api.jquery.com/jQuery.browser/
(you have to scroll down a bit)