jQuery 用jquery检测firefox浏览器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5698810/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 19:39:55  来源:igfitidea点击:

Detect firefox browser with jquery

jquerycssbrowser

提问by wyman

I facing a problem is my css is have a some bug when firefox is lower than 2.0. I would like to detect the browser to fix my css bug.

我面临的一个问题是当 Firefox 低于 2.0 时,我的 css 有一些错误。我想检测浏览器来修复我的 css 错误。

This is my code:

这是我的代码:

$(document).ready ( 
 function() {
  if ( $.browser.mozilla == true && $.browser.version < '3.0' ) {
   $('img.frameMargin').css('margin-left','35px');
  }
 }
);

But this code seem like not work.

但是这段代码似乎不起作用。

Thank for advance.

感谢提前。

采纳答案by wdm

You would have to do this...

你必须这样做......

$.browser.version < '1.9'

FireFox 2 should return 1.8... so this will pass.

FireFox 2 应该返回 1.8...所以这会通过。

回答by Jonathan Marzullo

This has been answered already in this discussion: In Javascript, how do I determine if my current browser is Firefox on a computer vs everything else?

这已经在这个讨论中得到了回答: 在 Javascript 中,我如何确定我当前的浏览器是计算机上的 Firefox 还是其他所有浏览器?

To answer your question from that post, thanks to "BalusC" for the answer:

要从该帖子中回答您的问题,感谢“BalusC”的回答:

var FF = !(window.mozInnerScreenX == null);
if(FF) {
    // is firefox 
} else { 
    // not firefox 
}

Above posts advise to use jQuery.browser. But the jQuery API recommends against using this method.. (see DOCS in API).

以上帖子建议使用 jQuery.browser。但是 jQuery API 建议不要使用这种方法..(请参阅API 中的 DOCS)。

jQuery API recommends to use 'jQuery.support' (http://api.jquery.com/jQuery.support/). The reason being is that jQuery.browseruses the user agent which can be spoofed and it is actually deprecated in later versions of jQuery. It's better to use feature detection instead of browser detection.

jQuery API 建议使用“jQuery.support”(http://api.jquery.com/jQuery.support/)。原因是它jQuery.browser使用了可以被欺骗的用户代理,它实际上在 jQuery 的后续版本中已被弃用。最好使用特征检测而不是浏览器检测。

UPDATE:

更新:

jQuery.browser is now removed from jQuery as of version 1.9

从 1.9 版开始,jQuery.browser 现在已从 jQuery 中删除

http://api.jquery.com/jQuery.browser/

http://api.jquery.com/jQuery.browser/

if you really need to use it.. jQuery.browser has been ported over as a separate jQuery plugin

如果你真的需要使用它.. jQuery.browser 已经作为一个单独的 jQuery 插件移植过来

https://github.com/gabceb/jquery-browser-plugin

https://github.com/gabceb/jquery-browser-plugin

hope that helps

希望有帮助

回答by krisztianf

I just bumped into this problem myself. $.browser.version actually detects the version of the rendering engine, and not the version of the browser (despite what its name implies), so if you want to detect Firefox 4and later, you should use

我自己刚刚遇到了这个问题。$.browser.version 实际上检测的是渲染引擎的版本,而不是浏览器的版本(尽管它的名字暗示了这一点),所以如果你想检测 Firefox 4及更高版本,你应该使用

$.browser.mozilla && $.browser.version > '2'

Because Firefox 4uses Gecko 2.0as the rendering engine. It's rather confusing, I know :)

因为 Firefox 4使用 Gecko 2.0作为渲染引擎。这相当令人困惑,我知道:)

回答by Zach Rattner

On my machine (Mac OS X 10.6), $.browser.versionin Firefox 4 reports "2.0". So, it looks like your code is valid, but there's an issue with JQuery.

在我的机器(Mac OS X 10.6)上,$.browser.version在 Firefox 4 中报告"2.0". 因此,看起来您的代码有效,但 JQuery 存在问题。

From the http://api.jquery.com/jQuery.browser/:

http://api.jquery.com/jQuery.browser/

We recommend against using this property; please try to use feature detection instead (see jQuery.support).

我们建议不要使用此属性;请尝试改用功能检测(请参阅 jQuery.support)。

If you need reliable browser detection in Javascript, check out one of the many tutorials online (such as this one).

如果您需要在 Javascript 中进行可靠的浏览器检测,请查看在线教程之一(例如教程)。