Home > OS >  Javascript: How to detect if the browser is Firefox?
Javascript: How to detect if the browser is Firefox?

Time:12-19

I was surprised to not be able to find a satisfying solution to this problem (in particular I'm looking for a solution that works for mobile phones as well). I tried something like this:

    if ($.browser.mozilla) {
        // do something
    } else {
        // do something else
    }

but obtained the following error:

Uncaught TypeError: Cannot read properties of undefined (reading 'mozilla')
    at (index):120:19

Thank you in advance for your help!

CodePudding user response:

You can get the user agent via navigator.userAgent. You could then use a regex or simply a .indexOf("BROWSERNAME") to check for different types of browsers.

CodePudding user response:

I found a solution that seems to work:

let userAgent = navigator.userAgent;
if(userAgent.match(/firefox|fxios/i))
{
   // do something
}
  • Related