64

昨日、いくつかのクライアントから、一部のコードが機能しなくなったという苦情がありました。jQuery.browserどうやらそれは、jQuery1.9がリリースされた昨日動作を停止した非推奨のプラグインを使用することに帰着します。

私は(すばやく)1.9の変更ドキュメントを調べましたが、その1つの関数の代わりにかなり重いライブラリを使用するように求められているようです。

その機能を復元するために推奨される最軽量のプラグインまたはコードスニペットはありますか?

これらのサイトが必要とするものについては、それは非常に基本的です。私は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

減価償却されたメソッドから離れるまで、更新することはできませんでした。

とにかくバージョン番号を指定せずに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 に答える
6

jQuery.browserからプラグインjquery.browserにロジックを抽象化しました。プラグインはMITライセンスの下でリリースされています。

IE11、Opera Webkit、Android検出のサポートも追加しました。

于 2013-08-26T21:12:16.297 に答える
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プラグインでjQuery.browser.msieを使用できるようにするだけでよい場合は、ここにワンライナーがあります。jQueryの後に含めるだけです。

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

これは最もばかげた可能性のある修正ですが、必要なのはそれだけで、機能するので、ここに行きます!

于 2013-09-03T11:59:00.647 に答える
1

この議論に追加します。「検出」の代わりに、ユーザーエージェントを使いやすいオブジェクトに解析するだけのjquery$.browserプラグインを思いついたところです。さらなるロジックを簡単に適用して、特定のブラウザーやプラットフォームをさらに細かく分析することができます。

UserAgentString.comで見つかったuseragentsで非常に信頼できる結果が得られました。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 に答える