64

昨天我有几个客户抱怨某些代码停止工作。显然,它归结为使用现已弃用的插件jQuery.browser,昨天 jQuery 1.9 发布时该插件停止工作。

我(很快)查看了 1.9 更改文档,似乎他们希望我用一些非常繁重的库来代替那个功能

是否有推荐的最轻量级插件或代码片段来恢复该功能?

对于这些网站的需求,这是非常基本的;我只需要对 IE 与 FF 与其他所有人进行最基本的检测。

建议?

4

13 回答 13

40

我使用了 Alexx Roche 回答的以下代码,但我想检测 MSIE:

<script type="text/javascript">
   $(document).ready(function() {
      if (navigator.userAgent.match(/msie/i) ){
        alert('I am an old fashioned Internet Explorer');
      }
   });
</script>

希望能帮助到你!

于 2013-04-24T14:31:03.607 回答
23

创建jQuery Migrate 是为了在您更新代码时允许向后兼容。

https://github.com/jquery/jquery-migrate

作为奖励,它会在您使用时记录已弃用的功能。在您解决问题时,我会尝试一下。此外,您应该为您的网站设置特定版本的 jQuery。升级很好,但一定要在将它们投入生产之前测试这些升级。如果您使用的是 CDN,您仍然可以在文件名中指定特定版本。

现在,您不需要 jQuery 插件来满足您的要求。检查对象_navigator

appCodeName: "Mozilla"
appName: "Netscape"
appVersion: "5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17"
cookieEnabled: true
doNotTrack: null
geolocation: Geolocation
language: "en-US"
mimeTypes: MimeTypeArray
onLine: true
platform: "MacIntel"
plugins: PluginArray
product: "Gecko"
productSub: "20030107"
userAgent: "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_6_8) AppleWebKit/537.17 (KHTML, like Gecko) Chrome/24.0.1312.52 Safari/537.17"
vendor: "Google Inc."
vendorSub: ""
于 2013-01-16T19:02:59.020 回答
22
var browser = {
        chrome: false,
        mozilla: false,
        opera: false,
        msie: false,
        safari: false
    };
    var sUsrAg = navigator.userAgent;
    if(sUsrAg.indexOf("Chrome") > -1) {
        browser.chrome = true;
    } else if (sUsrAg.indexOf("Safari") > -1) {
        browser.safari = true;
    } else if (sUsrAg.indexOf("Opera") > -1) {
        browser.opera = true;
    } else if (sUsrAg.indexOf("Firefox") > -1) {
        browser.mozilla = true;
    } else if (sUsrAg.indexOf("MSIE") > -1) {
        browser.msie = true;
    }
    console.log(browser.msie);
于 2013-05-07T01:37:35.757 回答
11

将此代码放在您的站点中(如 js 文件,或在 jQuery 代码之后...):

var matched, browser;

// Use of jQuery.browser is frowned upon.
// More details: http://api.jquery.com/jQuery.browser
// jQuery.uaMatch maintained for back-compat
jQuery.uaMatch = function( ua ) {
    ua = ua.toLowerCase();

    var match = /(chrome)[ \/]([\w.]+)/.exec( ua ) ||
        /(webkit)[ \/]([\w.]+)/.exec( ua ) ||
        /(opera)(?:.*version|)[ \/]([\w.]+)/.exec( ua ) ||
        /(msie) ([\w.]+)/.exec( ua ) ||
        ua.indexOf("compatible") < 0 && /(mozilla)(?:.*? rv:([\w.]+)|)/.exec( ua ) ||
        [];

    return {
        browser: match[ 1 ] || "",
        version: match[ 2 ] || "0"
    };
};

matched = jQuery.uaMatch( navigator.userAgent );
browser = {};

if ( matched.browser ) {
    browser[ matched.browser ] = true;
    browser.version = matched.version;
}

// Chrome is Webkit, but Webkit is also Safari.
if ( browser.chrome ) {
    browser.webkit = true;
} else if ( browser.webkit ) {
    browser.safari = true;
}

jQuery.browser = browser;
于 2013-01-24T10:24:32.407 回答
10

当我遇到同样的问题时,我使用了以下代码:

<script type="text/javascript">
 $(document).ready(function() {
    //if (!$.browser.webkit && ! $.browser.mozilla) { //depricated
    if (!navigator.userAgent.match(/mozilla/i) && 
        ! navigator.userAgent.match(/webkit/i) ){
        alert('Let me tell you about Mozilla');
    }
 });
</script>
于 2013-02-24T21:13:56.003 回答
6

我将 jQuery.browser 中的逻辑抽象为插件jquery.browser。该插件是在 MIT 许可下发布的。

我还添加了对 IE11、Opera Webkit 和 Android 检测的支持。

于 2013-08-26T21:12:16.297 回答
6

在您放弃折旧的方法之前,您可能无法更新。

你真的不应该在不指定版本号的情况下从 CDN 中包含 jquery,这在某种程度上违背了使用 CDN 的目的(无缓存)。

这是支持 $.browser 的最新版本的 jQuery 的链接:

http://code.jquery.com/jquery-1.8.3.min.js

只需用该链接替换您的 jquery.js src,您的代码将继续运行,直到您准备好继续前进并停止使用折旧的功能。

注意:Fancybox2 仍然使用 $.browser,这是自更新以来我见过的最常见的一个。

更新:Slickgrid 仍在使用$.browser,截至 2013 年 2 月 11 日没有更新

于 2013-01-16T19:13:01.603 回答
2

试试Conditionizr

希望这可以帮助 :)

于 2013-04-11T07:47:33.817 回答
2

可以通过额外检查特定 IE 版本中添加的标准全局对象的存在来检测 IE 的确切版本。

10 or older document.all
9 or older  document.all && !window.atob
8 or older  document.all && !document.addEventListener
7 or older  document.all && !document.querySelector
6 or older  document.all && !window.XMLHttpRequest
5.x document.all && !document.compatMode

if (document.all && !document.querySelector) {
    alert('IE7 or lower');
}

这些测试避免使用用于欺骗的 userAgent

于 2014-07-10T13:50:57.803 回答
1
if(!$.browser){
    $.browser={chrome:false,mozilla:false,opera:false,msie:false,safari:false};
    var ua=navigator.userAgent;
        $.each($.browser,function(c,a){
        $.browser[c]=((new RegExp(c,'i').test(ua)))?true:false;
            if($.browser.mozilla && c =='mozilla'){$.browser.mozilla=((new RegExp('firefox','i').test(ua)))?true:false;};
            if($.browser.chrome && c =='safari'){$.browser.safari=false;};
        });
};

http://jsfiddle.net/R3AAX/3/

于 2013-05-21T14:39:50.200 回答
1

如果您想要的只是能够使用 jQuery.browser.msie 的第 3 方 jQuery 插件,那么这里有一个单线。只需在 jQuery 之后包含它。

jQuery.browser = jQuery.browser || {msie: navigator.userAgent.match(/msie/i) ? true : false};

这是最愚蠢的解决方法,但这就是我所需要的,而且它确实有效,所以你去吧!

于 2013-09-03T11:59:00.647 回答
1

添加到这个讨论。我刚刚想出了一个 jquery $.browser 插件,它不是“检测”,只是将用户代理解析为一个易于使用的对象。可以轻松应用进一步的逻辑来进一步分解和解析特定的浏览器和平台。

我在这里找到了关于用户代理的非常可靠的结果:UserAgentString.com。我什至包括了 ie11 的版本检测(接近底部)。

//The following code is by no means perfect, nor is it meant to be a standalone 'detection' plugin. 
//It demonstrates parsing the useragent string into an easy to manage object. 
//Even if it does make detection rediculously easy.. :)

//Because this regex makes no assumptions in advance.
//IMO, It's compatibilty and maintainability is much higher than those based on static identifiers.

/*
  uaMatch replacement that parses a useragent string into an object
  useragent segments can be Name Value OR Name/Value OR Name

  Segment: Name Value
    Name: parsed to the last whitespace character
    Value: value after the last whitespace character
    Matches: (.NET CLR) (#.##), Android 2.3.4, etc
    Note: this regex can have leading/trailing whitespace (trimmed for object properties)

  Segment: Name/Value
    Matches: all values matching Name/Value
    Example: Firefox/24.0, Safari/533.1, Version/12.1, etc

  Segment: Name
    Matches: identifiers that hold no values (value of 'true' is implied)
    Example: Macintosh, Linux, Windows, KHTML, U, etc


   WARNING: not necessarily compatible with jQuery's $.browser implementation.
   - not recommended as a replacement for plugins that require it to function.
*/
(function ($) {

    var ua = navigator.userAgent.toLowerCase();

    var regex = /compatible; ([\w.+]+)[ \/]([\w.+]*)|([\w .+]+)[: \/]([\w.+]+)|([\w.+]+)/g;
    var match = regex.exec(ua);

    var browser = { };

    while (match != null) {
        var prop = {};

        if (match[1]) {
          prop.type = match[1];
          prop.version = match[2];
        }
        else if (match[3]) {
          prop.type = match[3];
          prop.version = match[4];
        }
        else {
          prop.type = match[5];
        }

        // some expressions have leading whitespace (i couldn't avoid this without a more complex expression)
        // trim them and get rid of '.' (' .NET CLR' = 'net_clr') 
        prop.type = $.trim(prop.type).replace(".","").replace(" ","_"); 
        var value = prop.version ? prop.version : true;

        if (browser[prop.type]) {
            if (!$.isArray(browser[prop.type]))
                browser[prop.type] = new Array(browser[prop.type]);

            browser[prop.type].push(value);
        }    
        else browser[prop.type] = value;

        match = regex.exec(ua);
    }

    for (var i in browser)
        if (i.indexOf("mac") > -1)
            browser.mac = true;

    if (browser.windows_nt && !browser.windows)
        browser.windows = true;

    //put known browsers into the 'version' property for 'some' jquery compatibility

    //for sake of argument chromium 'is' chrome
    if (browser.chromium && !browser.chrome)
        browser.chrome = browser.chromium;

    //chrome / safari / webkit
    if (browser.chrome) {
        browser.version = browser.chrome;
    }
    else if (browser.safari) {
        browser.version = browser.safari;
    }
    else {
        if (browser.applewebkit)
            browser.webkit = browser.applewebkit;

        if (browser.webkit)
            browser.version = browser.webkit;
    }

    //firefox / gecko
    if (browser.firefox) {
        if (browser.rv)
            browser.version = browser.rv;

        else browser.version = browser.firefox;
    }
    else if (browser.gecko) {
        if (browser.rv)
            browser.version = browser.rv;

        else browser.version = browser.gecko;
    }

    //opera
    if (browser.opera && !browser.version)
        browser.version = browser.opera;

    //msie
    if (browser.trident && browser.rv) //ie11
        browser.msie = browser.rv;

    if (browser.msie)
        browser.version = browser.msie;

    $.browser = browser;//Rename to reduce confliction?

    //WAS USED FOR TESTING & DISCOVERY (very useful)
    //TODO: remove line below
    alert(JSON.stringify($.browser));

}) (jQuery);

在 Internet Explorer 10 上,JSON.stringify 将输出如下内容:

{"mozilla":"5.0","msie":"10.0","windows_nt":"6.2","trident":"6.0","net4.0e":true,"net4.0c":true,"net_clr":["3.5.30729","2.0.50727","3.0.30729"],"windows":true,"version":"10.0"}
于 2013-10-23T23:23:51.630 回答
1

简短而强大。

// chrome, safari, webkit, mozilla, msie, opera
var chrome = /chrome/i.test(navigator.userAgent); 
于 2014-07-07T18:25:24.110 回答