繁体   English   中英

在 Javascript 中,如何确定我当前的浏览器是计算机上的 Firefox 还是其他浏览器?

[英]In Javascript, how do I determine if my current browser is Firefox on a computer vs everything else?

if(firefox and is on a computer){
alert('using firefox on a computer')
}else{
alert("using something else!");
}

我怎样才能做到这一点?

您所追求的是浏览器检测:

if ($.browser.mozilla) { ... 

但是,不鼓励浏览器嗅探,因为它很容易欺骗用户代理,即伪装成另一个浏览器!

您最好以自己的方式或通过jQuery.support接口使用功能检测: http : //api.jquery.com/jQuery.support/

这是一篇关于扩展它供您自己使用的文章: http : //www.waytoocrowded.com/2009/03/14/jquery-supportminheight/

编辑:

也找到了这篇文章,这有帮助: 当 IE8 不是 IE8 时,什么是 $.browser.version?

我正在做类似下面的事情;

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;
}

像这样:检查 Firefox。 或者其他一些浏览器。

 window.onload = function() {
          //  alert(navigator.userAgent);
            if (navigator.userAgent.indexOf("Firefox") > 0) {
                alert("ff");
            }
        }
if (navigator.userAgent.indexOf("Firefox") != -1) {

 //some specific code for Mozilla

 }
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(' ');
 })();

顾名思义,这就是浏览器所说的人- 但在要求它实际执行任何操作之前使用对象检测......

我使用它来记录来自用户的错误并在多个浏览器中测试代码 - 我知道 userAgent 字符串。

对于快速而肮脏的解决方案,只需执行以下操作,当您在NavigatorID.userAgent属性中搜索“Firefox”关键字时,使用 includes() 比 indexOf() 更干净。

const isFirefoxBrowser = navigator.userAgent.includes('Firefox');

警告:

基于检测用户代理字符串的浏览器识别不可靠,不推荐使用,因为用户代理字符串是用户可配置的。

在 MDN 上阅读有关 userAgent 的更多信息 >>

推荐解决方案:

使用特征检测而不是浏览器检测。

功能检测涉及确定浏览器是否支持某个代码块,并根据它是否支持(或不支持)运行不同的代码,以便浏览器始终可以提供工作体验,而不是在某些浏览器中崩溃/出错。

阅读有关在 MDN 上实现特征检测的更多信息 >>

最好检测您需要的功能,而不是浏览器。 例如,如果您需要知道是否支持 foo(),则可以使用 if(foo){} 进行检查

typeof InstallTrigger !== 'undefined'

它很简单并且在 Quantum 之后运行良好,但不确定这是否会面向未来:https ://developer.mozilla.org/en-US/docs/Web/API/InstallTrigger

http://api.jquery.com/jQuery.browser/

if ($.browser.mozilla) { ...

通常,您可以使用 javascript 的navigator.userAgentnavigator对象进行控制,

但是,如果您想使用现成的东西,请检查以下内容:

http://www.quirksmode.org/js/detect.html

希望这有帮助,思南。

正如评论中已经问过的那样:你为什么想要这个? 浏览器嗅探是一个坏习惯,只有少数情况需要它。

相反,使用特征检测。 正如Nicholas Zakas描述的,您应该在使用之前测试相对“不常见”的功能,并且只依赖这些测试,这样您就可以避免故障。 例如,做

if (window.XMLHttpRequest)
    var xhr = new XMLHttpRequest();

代替

if ((brwsr.IE && brwsr.IE.version >= 7) || (brwsr.firefox) || (brwsr.opera))
    var xhr = new XMLHttpRequest();

也不

if (window.XMLHttpRequest)
    // Hey, native XMLHttpRequest-support, so position: fixed is also supported

(相反,测试是否支持position: fixed

有几个不常见的浏览器,如KazehakaseMidori ,它们也可能支持,也可能不支持这些功能,因此在使用功能检测时,您的脚本将默默地处理它们。

但是请阅读提到的文章,因为它包含对这种技术的非常好的和彻底的解释。 (顺便说一句,我认为 Zakas为 Web 开发人员提供专业 JavaScript还是太不为人知了。)

这里提到的任何解决方案都不安全,因为代理并不总是首先提供正确的浏览器名称。 如果您使用 chrome 调用页面,则代理中也有 safari。 这意味着如果 safari 是 if 查询的第一件事,那么你在 elseif 上有 safari,如果你没有 elseif,并且只有在这种情况下覆盖被覆盖时才 safari。 这个解决方案不是最安全的,但它总是需要最小的索引来获得第一个。 Opera 正好相反,opera 位于代理的最后面,因此其他浏览器可能会有所不同。

/**
 * 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();
});

并与 CSS 一起使用以在浏览器上设置样式:

[data-useragent="firefox"] {
    /* Attribute has this exact value */
}

您可以为此使用navigator.userAgent 看看它是否包含Mozilla

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM