我如何使用jQuery检测浏览器类型? [英] How can I detect browser type using jQuery?
本文介绍了我如何使用jQuery检测浏览器类型?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我想,如果用户正在使用IE和Firefox来检测,但我无法找到脚本。
我有code如下:
$(文件)。就绪(函数(五){
。$ .browser.chrome = / CHROM(E | IUM)/测试(navigator.userAgent.toLowerCase());
如果($。browser.chrome){
警报(1);
//这个工作做好
}
否则如果(//该浏览器是IE){警报(2);}
否则,如果(//浏览器是火狐){警报(3);} //问题是,我不知道怎么写了IE和FireFox浏览器Chrome浏览器的脚本是做工精细
)};
解决方案
最好的解决方案可能是:使用Modernizr的
不过,如果你一定要使用$ .browser属性,你可以用它做 jQuery的迁移插件(jQuery的> = 1.9 - 在早期版本中你可以使用它),然后像做:
如果($。browser.chrome){
警报(1);
}否则如果($ .browser.mozilla){
警报(2);
}否则如果($ .browser.msie){
警报(3);
}
如果你需要一些理由使用navigator.userAgent的,那么这将是:
$ browser.msie = /msie/.test(navigator.userAgent.toLowerCase());
$ .browser.mozilla = /firefox/.test(navigator.userAgent.toLowerCase());
I want to detect if the user is using IE and Firefox but I cannot find the script.
I have code as below:
$(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
)};
解决方案
The best solution is probably: use Modernizr.
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);
}
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屋!
查看全文