2013-10-14 30 views
20

我想檢測用戶是否使用IE和Firefox,但我找不到腳本。如何使用jQuery檢測瀏覽器類型?

我有如下代碼:

$(document).ready(function(e) { 
    $.browser.chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); 
    if($.browser.chrome){ 
     alert(1); 
      //this work well 
    } 
      else if(//the browser is IE){alert(2);} 
      else if(//the browser is Firefox){alert(3);} 

    //The problem is that I don't know how to write a script for IE and FireFox browser for chrome is work fine 
)}; 
+0

如果你依賴'$ .browser'那麼肯定_you_不需要在你的代碼中實現瀏覽器檢測。無論哪種情況,都應考慮使用功能檢測。 – WynandB

+1

如上所述,在https://api.jquery.com/jquery.browser/上,「該屬性在jQuery 1.9中被刪除,只能通過jQuery.migrate插件使用,請嘗試使用特徵檢測。」 –

回答

27

最好的解決方案可能是:使用Modernizr。

if($.browser.chrome) { 
    alert(1); 
} else if ($.browser.mozilla) { 
    alert(2); 
} else if ($.browser.msie) { 
    alert(3); 
} 
: - (在早期版本中你可以使用它的jQuery> = 1.9),然後像做

不過,如果你一定要使用$ .browser屬性,你可以用它jQuery Migrate插件做

如果你需要一些理由來使用navigator.userAgent的,那麼這將是:

$.browser.msie = /msie/.test(navigator.userAgent.toLowerCase()); 
$.browser.mozilla = /firefox/.test(navigator.userAgent.toLowerCase()); 
+0

可以在任何瀏覽器中使用Modernizr和jQuery Migrate Plugin,例如:IE6和更舊的瀏覽器,因爲這是我們使用瀏覽器檢測功能的主要目的之一。請提出建議。 –

+8

$ .browser已從2013-01-15發佈的所有jQuery 1.9+版本中刪除。 –

+0

我在IE 11中得到這個,「無法獲取未定義的屬性'mozilla'或空引用」 – eaglei22

6

你不應該寫自己的瀏覽器檢測代碼 - 它已經做過很多次。使用Modernizr來檢測獨立的瀏覽器功能。檢測各種功能比檢測整個瀏覽器更好,因爲各種瀏覽器可能支持不同的功能集,而且這些功能甚至可能會通過同一瀏覽器的各種版本進行更改。如果您檢測到某個特定功能的存在,那麼您的代碼在更多瀏覽器中可能會更好。對於各種移動瀏覽器尤其如此。

運行Modernizr時,它會更新HEAD元素的class屬性,以便列出您正在使用的瀏覽器的各種功能 - 然後可以使用Javascript查詢屬性並決定如果功能存在(或缺失)。

18

我對檢測解決方案:

if (navigator.userAgent.match(/msie/i) || navigator.userAgent.match(/trident/i)){ 
    $("html").addClass("ie"); 
} 

Jquery需要。

+0

這可以通過檢測所有msIE瀏覽器版本來工作。 你是否有一個方便的變化來檢測,比如IE8和IE9,而不是IE7,IE10或IE11? – j4v1

+2

我向你推薦這個:http://idiallo.com/blog/detect-ie-version-with-js –

+0

提供的答案需要在html中添加內容,我並沒有真正考慮過。我確實發現了這個其他文章:http://tanalin.com/en/articles/ie-version-js/ – j4v1

-1

我已經使用了它,它適用於我。還包括jQuery的遷移插件和jquery文件。

if ($.browser.webkit) { 
alert("This is WebKit!"); 
} 
1

嘗試使用它

$(document).ready(function() { 
// If the browser type if Mozilla Firefox 
if ($.browser.mozilla && $.browser.version >= "1.8"){ 
// some code 
} 
// If the browser type is Opera 
if($.browser.opera) 
{ 
// some code 
} 
// If the web browser type is Safari 
if($.browser.safari) 
{ 
// some code 
} 
// If the web browser type is Chrome 
if($.browser.chrome) 
{ 
// some code 
} 
// If the web browser type is Internet Explorer 
if ($.browser.msie && $.browser.version <= 6) 
{ 
// some code 
} 
//If the web browser type is Internet Explorer 6 and above 
if ($.browser.msie && $.browser.version > 6) 
{ 
// some code 
} 
}); 
+0

@cyberroot您說的代碼不起作用 – Techy

+0

您測試了哪個瀏覽器@Techy – cyberoot

+0

chrome os ,我也在小提琴裏面檢查過這段代碼。 – Techy

3

使用此:

(function (factory) { 
    if (typeof define === 'function' && define.amd) { 
    // AMD. Register as an anonymous module. 
    define(['jquery'], function ($) { 
     return factory($); 
    }); 
    } else if (typeof module === 'object' && typeof module.exports === 'object') { 
    // Node-like environment 
    module.exports = factory(require('jquery')); 
    } else { 
    // Browser globals 
    factory(window.jQuery); 
    } 
}(function(jQuery) { 
    "use strict"; 

    function uaMatch(ua) { 
    // If an UA is not provided, default to the current browser UA. 
    if (ua === undefined) { 
     ua = window.navigator.userAgent; 
    } 
    ua = ua.toLowerCase(); 

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

    var platform_match = /(ipad)/.exec(ua) || 
     /(ipod)/.exec(ua) || 
     /(iphone)/.exec(ua) || 
     /(kindle)/.exec(ua) || 
     /(silk)/.exec(ua) || 
     /(android)/.exec(ua) || 
     /(windows phone)/.exec(ua) || 
     /(win)/.exec(ua) || 
     /(mac)/.exec(ua) || 
     /(linux)/.exec(ua) || 
     /(cros)/.exec(ua) || 
     /(playbook)/.exec(ua) || 
     /(bb)/.exec(ua) || 
     /(blackberry)/.exec(ua) || 
     []; 

    var browser = {}, 
     matched = { 
      browser: match[ 5 ] || match[ 3 ] || match[ 1 ] || "", 
      version: match[ 2 ] || match[ 4 ] || "0", 
      versionNumber: match[ 4 ] || match[ 2 ] || "0", 
      platform: platform_match[ 0 ] || "" 
     }; 

    if (matched.browser) { 
     browser[ matched.browser ] = true; 
     browser.version = matched.version; 
     browser.versionNumber = parseInt(matched.versionNumber, 10); 
    } 

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

    // These are all considered mobile platforms, meaning they run a mobile browser 
    if (browser.android || browser.bb || browser.blackberry || browser.ipad || browser.iphone || 
     browser.ipod || browser.kindle || browser.playbook || browser.silk || browser[ "windows phone" ]) { 
     browser.mobile = true; 
    } 

    // These are all considered desktop platforms, meaning they run a desktop browser 
    if (browser.cros || browser.mac || browser.linux || browser.win) { 
     browser.desktop = true; 
    } 

    // Chrome, Opera 15+ and Safari are webkit based browsers 
    if (browser.chrome || browser.opr || browser.safari) { 
     browser.webkit = true; 
    } 

    // IE11 has a new token so we will assign it msie to avoid breaking changes 
    // IE12 disguises itself as Chrome, but adds a new Edge token. 
    if (browser.rv || browser.edge) { 
     var ie = "msie"; 

     matched.browser = ie; 
     browser[ie] = true; 
    } 

    // Blackberry browsers are marked as Safari on BlackBerry 
    if (browser.safari && browser.blackberry) { 
     var blackberry = "blackberry"; 

     matched.browser = blackberry; 
     browser[blackberry] = true; 
    } 

    // Playbook browsers are marked as Safari on Playbook 
    if (browser.safari && browser.playbook) { 
     var playbook = "playbook"; 

     matched.browser = playbook; 
     browser[playbook] = true; 
    } 

    // BB10 is a newer OS version of BlackBerry 
    if (browser.bb) { 
     var bb = "blackberry"; 

     matched.browser = bb; 
     browser[bb] = true; 
    } 

    // Opera 15+ are identified as opr 
    if (browser.opr) { 
     var opera = "opera"; 

     matched.browser = opera; 
     browser[opera] = true; 
    } 

    // Stock Android browsers are marked as Safari on Android. 
    if (browser.safari && browser.android) { 
     var android = "android"; 

     matched.browser = android; 
     browser[android] = true; 
    } 

    // Kindle browsers are marked as Safari on Kindle 
    if (browser.safari && browser.kindle) { 
     var kindle = "kindle"; 

     matched.browser = kindle; 
     browser[kindle] = true; 
    } 

    // Kindle Silk browsers are marked as Safari on Kindle 
    if (browser.safari && browser.silk) { 
     var silk = "silk"; 

     matched.browser = silk; 
     browser[silk] = true; 
    } 

    // Assign the name and platform variable 
    browser.name = matched.browser; 
    browser.platform = matched.platform; 
    return browser; 
    } 

    // Run the matching process, also assign the function to the returned object 
    // for manual, jQuery-free use if desired 
    window.jQBrowser = uaMatch(window.navigator.userAgent); 
    window.jQBrowser.uaMatch = uaMatch; 

    // Only assign to jQuery.browser if jQuery is loaded 
    if (jQuery) { 
    jQuery.browser = window.jQBrowser; 
    } 

    return window.jQBrowser; 
})); 
0

另一種方式找到IE

http://tanalin.com/en/articles/ie-version-js/

IE版本條件T版本Ø檢查

IE 10 or older - document.all <BR/> 
IE 9 or older - document.all && !window.atob <br/> 
IE 8 or older - document.all && !document.addEventListener <br/> 
IE 7 or older - document.all && !document.querySelector <br/> 
IE 6 or older - document.all && !window.XMLHttpRequest <br/> 
IE 5.x   - document.all && !document.compatMode 
0
$.browser.chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); 

if($.browser.chrome){ 
    alert(1);  
} 

UPDATE:(10倍到@Mr。 Bacciagalupe)

jQuery已刪除$.browser1.9及其最新版本。

但是你仍然可以使用$。瀏覽器作爲一個獨立的插件,發現here