如何使用jQuery检测浏览器类型?

我想检测用户是否使用IE和Firefox,但我找不到脚本。

我的代码如下:

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

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

但是,如果你一定想使用$ .browser属性,你可以使用jQuery Migrate插件(对于JQuery> = 1.9 – 在早期版本中你可以使用它),然后执行以下操作:

 if($.browser.chrome) { alert(1); } else if ($.browser.mozilla) { alert(2); } else if ($.browser.msie) { alert(3); } 

如果您因某种原因需要使用navigator.userAgent,那么它将是:

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

我的ie检测解决方案:

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

Jquery需要。

你不应该编写自己的浏览器检测代码 – 之前已经多次完成了。 使用Modernizr来检测独立的浏览器function。 检测各种function比检测整个浏览器更好,因为各种浏览器可能支持不同的function集,而这些function甚至可能通过同一浏览器的各种版本进行更改。 如果您检测到某个特定function的存在,您的代码可能会在更多浏览器中更好地工作。 对于各种移动浏览器尤其如此。

当您运行Modernizr时,它将更新您的HEAD元素的class属性,以便列出您正在使用的浏览器的各种function – 然后您可以使用Javascript查询属性并决定在存在function时该怎么做(或失踪)。

用这个:

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

尝试使用它

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

您可以使用此代码查找正确的浏览器,您可以对任何目标浏览器进行更改…..

 function myFunction() { if((navigator.userAgent.indexOf("Opera") || navigator.userAgent.indexOf('OPR')) != -1 ){ alert('Opera'); } else if(navigator.userAgent.indexOf("Chrome") != -1 ){ alert('Chrome'); } else if(navigator.userAgent.indexOf("Safari") != -1){ alert('Safari'); } else if(navigator.userAgent.indexOf("Firefox") != -1 ){ alert('Firefox'); } else if((navigator.userAgent.indexOf("MSIE") != -1 ) || (!!document.documentMode == true )){ alert('IE'); } else{ alert('unknown'); } } 
    Browser detector   // your code here   

另一种查找IE版本的方法

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

IE版本要检查的条件

 IE 10 or older - document.all 
IE 9 or older - document.all && !window.atob
IE 8 or older - document.all && !document.addEventListener
IE 7 or older - document.all && !document.querySelector
IE 6 or older - document.all && !window.XMLHttpRequest
IE 5.x - document.all && !document.compatMode
 $.browser.chrome = /chrom(e|ium)/.test(navigator.userAgent.toLowerCase()); if($.browser.chrome){ alert(1); } 

更新:(10x到@Mr.Bacciagalupe)

jQuery从1.9及其最新版本中删除了$.browser

但你仍然可以使用$ .browser作为独立的插件,在这里找到

我已经使用过它,它适用于我。还包括jquery migrate插件和jquery文件。

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