Detect firefox browser with jquery

前端 未结 4 1551
臣服心动
臣服心动 2021-02-13 11:14

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.

This is my code:

$(         


        
相关标签:
4条回答
  • 2021-02-13 11:48

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

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

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

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

    0 讨论(0)
  • 2021-02-13 11:52

    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?

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

    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 API recommends to use 'jQuery.support' (http://api.jquery.com/jQuery.support/). The reason being is that jQuery.browser uses 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.

    UPDATE:

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

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

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

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

    hope that helps

    0 讨论(0)
  • 2021-02-13 12:04

    You would have to do this...

    $.browser.version < '1.9'
    

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

    0 讨论(0)
  • 2021-02-13 12:06

    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 4 and later, you should use

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

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

    0 讨论(0)
提交回复
热议问题