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

后端 未结 13 1868
萌比男神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: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 >>

提交回复
热议问题