如何使用 jQuery 检测浏览器类型? [英] How can I detect browser type using jQuery?

查看:27
本文介绍了如何使用 jQuery 检测浏览器类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I want to detect if the user is using IE and Firefox but I cannot find the script.

我有如下代码:

$(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.

The best solution is probably: use Modernizr.

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

However, if you necessarily want to use $.browser property, you can do it using jQuery Migrate plugin (for JQuery >= 1.9 - in earlier versions you can just use it) and then do something like:

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

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

And if you need for some reason to use navigator.userAgent, then it would be:

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

这篇关于如何使用 jQuery 检测浏览器类型?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆