在 Javascript 中,如何确定我当前的浏览器是计算机上的 Firefox 还是其他浏览器?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2324944/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
In Javascript, how do I determine if my current browser is Firefox on a computer vs everything else?
提问by TIMEX
if(firefox and is on a computer){
alert('using firefox on a computer')
}else{
alert("using something else!");
}
How can I do this?
我怎样才能做到这一点?
回答by James Wiseman
What you're after is known as browser detection:
您所追求的是浏览器检测:
if ($.browser.mozilla) { ...
However, browser sniffing is discouraged, as its easy to spoof the user agent, i.e. pretend to be another browser!
但是,不鼓励浏览器嗅探,因为它很容易欺骗用户代理,即伪装成另一个浏览器!
You'd best use feature detection, either in your own way, or through the jQuery.supportinterface: http://api.jquery.com/jQuery.support/
您最好以自己的方式或通过jQuery.support界面使用功能检测:http: //api.jquery.com/jQuery.support/
Here's an article on extending it for your own use: http://www.waytoocrowded.com/2009/03/14/jquery-supportminheight/
这是一篇关于扩展它供您自己使用的文章:http: //www.waytoocrowded.com/2009/03/14/jquery-supportminheight/
Edit:
编辑:
Found this post as well which helps: When IE8 is not IE8 what is $.browser.version?
也找到了这篇文章,这有帮助:当 IE8 不是 IE8 时,什么是 $.browser.version?
回答by Fayyaz Ali
I am doing some thing like below;
我正在做类似下面的事情;
function checkBrowser(){
c = navigator.userAgent.search("Chrome");
f = navigator.userAgent.search("Firefox");
m8 = navigator.userAgent.search("MSIE 8.0");
m9 = navigator.userAgent.search("MSIE 9.0");
if (c > -1) {
browser = "Chrome";
} else if (f > -1) {
browser = "Firefox";
} else if (m9 > -1) {
browser ="MSIE 9.0";
} else if (m8 > -1) {
browser ="MSIE 8.0";
}
return browser;
}
回答by poo
Like this: Check for Firefox. Or some other browser.
像这样:检查 Firefox。或者其他一些浏览器。
window.onload = function() {
// alert(navigator.userAgent);
if (navigator.userAgent.indexOf("Firefox") > 0) {
alert("ff");
}
}
回答by David Castro
if (navigator.userAgent.indexOf("Firefox") != -1) {
//some specific code for Mozilla
}
回答by kennebec
navigator.sayswho= (function(){
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.join(' ');
})();
as the name suggests, this is who the browser saysit is- but use object detection before asking it to actually do anything...
顾名思义,这就是浏览器所说的人- 但在要求它实际执行任何操作之前使用对象检测......
I use it for logging errors from users and in testing code in multiple browsers- where I know the userAgent strings.
我使用它来记录来自用户的错误并在多个浏览器中测试代码 - 我知道 userAgent 字符串。
回答by webpreneur
For a quick and dirty solution just do the following, it is cleaner to use includes() when you are searching the 'Firefox' keyword in the NavigatorID.userAgentproperty than indexOf().
对于快速而肮脏的解决方案,只需执行以下操作,在NavigatorID.userAgent属性中搜索“Firefox”关键字时使用 includes()比 indexOf()更干净。
const isFirefoxBrowser = navigator.userAgent.includes('Firefox');
WARNING:
警告:
Browser identification based on detecting the user agent string is unreliable and is not recommended, as the user agent string is user configurable.
基于检测用户代理字符串的浏览器识别不可靠,不推荐使用,因为用户代理字符串是用户可配置的。
Read more about userAgent on MDN >>
在 MDN 上阅读有关 userAgent 的更多信息 >>
RECOMMENDED SOLUTION:
推荐解决方案:
Use feature detection instead of browser detection.
使用特征检测而不是浏览器检测。
Feature detection involves working out whether a browser supports a certain block of code, and running different code dependent on whether it does (or doesn't), so that the browser can always provide a working experience rather crashing/erroring in some browsers.
功能检测涉及确定浏览器是否支持某个代码块,并根据它是否支持(或不支持)运行不同的代码,以便浏览器始终可以提供工作体验,而不是在某些浏览器中崩溃/出错。
回答by Sam Dark
It's better to detect features you need, not a browser. For example, if you need to know if foo() is supported, you can check it with if(foo){}
最好检测您需要的功能,而不是浏览器。例如,如果您需要知道是否支持 foo(),则可以使用 if(foo){} 进行检查
回答by goodhyun
typeof InstallTrigger !== 'undefined'
It's simple and works well after Quantum but not sure whether this will be future-proof though: https://developer.mozilla.org/en-US/docs/Web/API/InstallTrigger
它很简单并且在 Quantum 之后运行良好,但不确定这是否会面向未来:https: //developer.mozilla.org/en-US/docs/Web/API/InstallTrigger
回答by Marcel Korpel
As already asked in a comment: why do you want this? Browser sniffing is a bad habit and there are only a few situations where it is needed.
正如评论中已经问过的那样:你为什么想要这个?浏览器嗅探是一个坏习惯,只有少数情况需要它。
Instead, use feature detection. As described by Nicholas Zakas, you should test relatively 'uncommon' features before using them and only rely on these tests, so you're kind of fail-safe. For example, do
相反,使用特征检测。正如Nicholas Zakas所描述的,您应该在使用之前测试相对“不常见”的功能,并且只依赖这些测试,这样您就可以避免故障。例如,做
if (window.XMLHttpRequest)
var xhr = new XMLHttpRequest();
instead of
代替
if ((brwsr.IE && brwsr.IE.version >= 7) || (brwsr.firefox) || (brwsr.opera))
var xhr = new XMLHttpRequest();
And also don'tdo
也不要做
if (window.XMLHttpRequest)
// Hey, native XMLHttpRequest-support, so position: fixed is also supported
(instead, test if position: fixedis supported)
(相反,测试是否position: fixed支持)
There exist several uncommon browsers with names like Kazehakaseand Midorithat also might, or might not, support these features, so your scripts will silently work on them when using feature detection.
有几种不常见的浏览器,如Kazehakase和Midori,它们也可能支持,也可能不支持这些功能,因此在使用功能检测时,您的脚本将默默地处理它们。
But please read the mentioned article, as it contains a very good and thorough explanation of this technique. (By the way, I think that Zakas' Professional JavaScript for Web Developersis still too unknown.)
但是请阅读提到的文章,因为它包含对这种技术的非常好的和彻底的解释。(顺便说一句,我认为 Zakas为 Web 开发人员提供的专业 JavaScript还是太不为人知了。)
回答by Franco
http://api.jquery.com/jQuery.browser/
http://api.jquery.com/jQuery.browser/
if ($.browser.mozilla) { ...

