在Javascript中,如何确定当前浏览器是计算机上的Firefox还是其他浏览器?
我如何才能做到这一点?您可以使用在Javascript中,如何确定当前浏览器是计算机上的Firefox还是其他浏览器?,javascript,jquery,Javascript,Jquery,我如何才能做到这一点?您可以使用navigator.userAgent来实现这一点。看看它是否包含Mozilla 通常,您可以使用javascript的navigator.userAgent或navigator对象创建控件 但如果您想使用准备就绪的产品,请检查以下内容: 希南,希望这能有所帮助。最好检测您需要的功能,而不是浏览器。例如,如果您需要知道是否支持foo(),可以使用if(foo){}检查它,您所追求的是浏览器检测: if ($.browser.mozilla) { ... 但是,
navigator.userAgent
来实现这一点。看看它是否包含Mozilla
通常,您可以使用javascript的
navigator.userAgent
或navigator
对象创建控件
但如果您想使用准备就绪的产品,请检查以下内容:
希南,希望这能有所帮助。最好检测您需要的功能,而不是浏览器。例如,如果您需要知道是否支持foo(),可以使用if(foo){}检查它,您所追求的是浏览器检测:
if ($.browser.mozilla) { ...
但是,不鼓励浏览器嗅探,因为它很容易欺骗用户代理,即假装是另一个浏览器
您最好以自己的方式或通过jQuery使用功能检测。支持
界面:
下面是一篇关于扩展它以供您自己使用的文章:
编辑:
也找到了这篇文章,它有助于:
顾名思义,这就是浏览器所说的
但是在要求它实际做任何事情之前使用物体检测
我使用它来记录用户的错误,并在多个浏览器中测试代码——我知道userAgent字符串。如下:检查Firefox。或者其他浏览器
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(' ');
})();
正如已经在评论中被问到的:你为什么想要这个?浏览器嗅探是一个坏习惯,只有少数情况下需要它 相反,使用特征检测。同样,您应该在使用相对“不常见”的功能之前对其进行测试,并且只依赖于这些测试,所以您有点故障安全。例如,做什么
window.onload = function() {
// alert(navigator.userAgent);
if (navigator.userAgent.indexOf("Firefox") > 0) {
alert("ff");
}
}
而不是
if (window.XMLHttpRequest)
var xhr = new XMLHttpRequest();
还有不要这样做
if ((brwsr.IE && brwsr.IE.version >= 7) || (brwsr.firefox) || (brwsr.opera))
var xhr = new XMLHttpRequest();
(相反,如果支持位置:fixed
,则进行测试)
存在几种不常见的浏览器,其名称为and,也可能支持或不支持这些功能,因此在使用功能检测时,您的脚本将以静默方式处理这些功能
但请阅读,因为它包含了一个非常好的和彻底的解释这项技术。(顺便说一句,我认为扎卡斯的名字还太不为人所知。)我正在做下面的事情
if (window.XMLHttpRequest)
// Hey, native XMLHttpRequest-support, so position: fixed is also supported
这里提到的任何解决方案都是不安全的,因为代理首先并不总是提供正确的浏览器名称。如果您使用chrome调用页面,那么代理中也有safari。这意味着如果safari是if查询中首先发生的事情,那么您在elseif上有safari如果您没有elseif,并且只有在这种情况下覆盖safari。这个解决方案不是最安全的,但它总是需要最小的索引才能得到第一个。Opera则相反,Opera位于代理的最后面,因此其他浏览器可能会有所不同
if (navigator.userAgent.indexOf("Firefox") != -1) {
//some specific code for Mozilla
}
并与CSS一起使用以在浏览器上设置样式:
document.addEventListener('DOMContentLoaded', function () {
_MBT_Browser.__init();
});
对于快速而肮脏的解决方案,只需执行以下操作,在搜索
NavigatorID.userAgent
属性中的'Firefox'关键字时使用includes(),比使用indexOf()更干净
警告:
基于检测用户代理字符串的浏览器识别是
不可靠,不建议使用,因为用户代理字符串是user
可配置
推荐解决方案:
使用特征检测而不是浏览器检测
特征检测涉及确定浏览器是否支持
特定的代码块,并且运行不同的代码取决于
它可以(也可以不可以),因此浏览器可以始终提供工作状态
在某些浏览器中遇到相当多的崩溃/错误
它很简单,在Quantum之后运行良好,但不确定这是否是未来的证明:
什么是“在电脑上”?你想同时检测iPhone和其他移动设备吗?为什么你想知道?因为q上说如果firefox和其他设备在电脑上?计算机上的数据一定很重要,否则它就不存在了。。毕竟它是多余的,否则。你的意思是泄气+1对于功能检测,请用“正确的方式”进行:@Marcel-Korpel:顺便说一句,这篇文章写得很好。我意识到我没有正确理解特征检测。你有没有在回信中写上?因为我很乐意投赞成票!我不同意你的“正确的方法”。欺骗用户代理并不是出于恶意,而是为了解决开发人员的bug。所以最好相信用户想要什么,你认为他是。。他比你有更好的理由这被贬低并从最新的jQueryIt输出中删除:“TypeError:$.browser未定义”,因为这个属性是从最新版本的jQuery中删除的。几乎所有东西都包括字符串Mozilla…包括iPhone上的Safari!这对于确定是否正在使用Firefox是无用的,对于确定是否正在使用计算机也是无用的!看看臭名昭著的Mozilla标签的详细历史。如果这一直有效的话,那就太好了。但有时您会遇到这样的情况,即功能在不同浏览器中的实现略有不同,但这种差异不会在变量中声明。例如,我试图解决一个问题,即不能在firefox中复制禁用字段中的文本,但可以在其他浏览器中复制。因此,我需要切换到使用firefox中的readOnly属性。除了了解用户代理之外,无法检测此问题。此属性已从jQueryWhile中删除,而此代码可能会回答此问题,但添加代码解释将有助于将来的读者。您正在引入全局变量,这看起来不像是检查此问题的可靠方法。例如,这会将Edge检测为“Chrome”,请查看我的答案日期,稍后介绍。您不需要
let
来避免全局变量,您可以使用var
,因为JavaScript的历史已经开始。Edge浏览器说它是“Chrome 88.0.4324.190”
。奇怪的
function checkBrowser(){
let browser = "";
let c = navigator.userAgent.search("Chrome");
let f = navigator.userAgent.search("Firefox");
let m8 = navigator.userAgent.search("MSIE 8.0");
let 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;
}
if (navigator.userAgent.indexOf("Firefox") != -1) {
//some specific code for Mozilla
}
/**
* Description of Browser
*
* @author Samet Tarim
* @link http://www.tnado.com/
*/
var _MBT_Browser = {
client: {},
__init: function () {
console.log('Init Browser...');
_MBT_Browser.client.agent = navigator.userAgent;
_MBT_Browser.set();
},
set: function () {
var browsers = ["Firefox", "MSIE", "Trident", "Safari", "Chrome", "OPR"]; // Set the browser we want to detect
_MBT_Browser.get(browsers);
if (_MBT_Browser.client.current) {
document.documentElement.setAttribute('data-useragent', _MBT_Browser.client.current); // Ad HTML tag data-useragent="IE"
}
},
get: function (browser) {
var index = '',
i = 0,
max = 0,
min = 1000000; // to get the min value, we set first to million, is a placeholder for the first loop
for (; i < browser.length; i++) {
index = _MBT_Browser.client.agent.indexOf(browser[i]); // get index
// Special case, we need here the largest index
if (browser[i] === "OPR") {
if (index > -1 && index > max) {
min = index;
_MBT_Browser.client.current = browser[i].toLowerCase();
}
} else {
// We hold only the smallest index number and overwrite on smaller
if (index > -1 && index < min) {
min = index;
_MBT_Browser.client.current = browser[i].toLowerCase();
}
}
}
}
};
document.addEventListener('DOMContentLoaded', function () {
_MBT_Browser.__init();
});
[data-useragent="firefox"] {
/* Attribute has this exact value */
}
const isFirefoxBrowser = navigator.userAgent.includes('Firefox');
typeof InstallTrigger !== 'undefined'