In Javascript, how do I determine if my current browser is Firefox on a computer vs everything else?

后端 未结 13 1858
萌比男神i
萌比男神i 2020-12-01 07:13
if(firefox and is on a computer){
alert(\'using firefox on a computer\')
}else{
alert(\"using something else!\");
}

How can I do this?

相关标签:
13条回答
  • 2020-12-01 07:49

    What you're after is known as browser detection:

    if ($.browser.mozilla) { ... 
    

    However, browser sniffing is discouraged, as its easy to spoof the user agent, i.e. pretend to be another browser!

    You'd best use feature detection, either in your own way, or through the jQuery.support interface: http://api.jquery.com/jQuery.support/

    Here's an article on extending it for your own use: http://www.waytoocrowded.com/2009/03/14/jquery-supportminheight/

    Edit:

    Found this post as well which helps: When IE8 is not IE8 what is $.browser.version?

    0 讨论(0)
  • 2020-12-01 07:51

    For a quick and dirty solution just do the following, it is cleaner to use includes() when you are searching the 'Firefox' keyword in the NavigatorID.userAgent property than indexOf().

    const isFirefoxBrowser = navigator.userAgent.includes('Firefox');
    

    WARNING:

    Browser identification based on detecting the user agent string is unreliable and is not recommended, as the user agent string is user configurable.

    Read more about userAgent on MDN >>

    RECOMMENDED SOLUTION:

    Use feature detection instead of browser detection.

    Feature detection involves working out whether a browser supports a certain block of code, and running different code dependent on whether it does (or doesn't), so that the browser can always provide a working experience rather crashing/erroring in some browsers.

    Read more about implementing feature detection on MDN >>

    0 讨论(0)
  • 2020-12-01 07:51
    typeof InstallTrigger !== 'undefined'
    

    It's simple and works well after Quantum but not sure whether this will be future-proof though: https://developer.mozilla.org/en-US/docs/Web/API/InstallTrigger

    0 讨论(0)
  • 2020-12-01 07:55

    I am doing some thing like below;

    function checkBrowser(){
        let browser = "";
        let c = navigator.userAgent.search("Chrome");
        let f = navigator.userAgent.search("Firefox");
        let m8 = navigator.userAgent.search("MSIE 8.0");
        let m9 = navigator.userAgent.search("MSIE 9.0");
        if (c > -1) {
            browser = "Chrome";
        } else if (f > -1) {
            browser = "Firefox";
        } else if (m9 > -1) {
            browser ="MSIE 9.0";
        } else if (m8 > -1) {
            browser ="MSIE 8.0";
        }
        return browser;
    }
    
    0 讨论(0)
  • 2020-12-01 07:56

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

    if ($.browser.mozilla) { ...
    
    0 讨论(0)
  • 2020-12-01 07:57

    As already asked in a comment: why do you want this? Browser sniffing is a bad habit and there are only a few situations where it is needed.

    Instead, use feature detection. As described by Nicholas Zakas, you should test relatively 'uncommon' features before using them and only rely on these tests, so you're kind of fail-safe. For example, do

    if (window.XMLHttpRequest)
        var xhr = new XMLHttpRequest();
    

    instead of

    if ((brwsr.IE && brwsr.IE.version >= 7) || (brwsr.firefox) || (brwsr.opera))
        var xhr = new XMLHttpRequest();
    

    And also don't do

    if (window.XMLHttpRequest)
        // Hey, native XMLHttpRequest-support, so position: fixed is also supported
    

    (instead, test if position: fixed is supported)

    There exist several uncommon browsers with names like Kazehakase and Midori that also might, or might not, support these features, so your scripts will silently work on them when using feature detection.

    But please read the mentioned article, as it contains a very good and thorough explanation of this technique. (By the way, I think that Zakas' Professional JavaScript for Web Developers is still too unknown.)

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