9

I'm using the following code to detect whether the browser being used on my mobile site matches a certain crieteria:

var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
if (isiPhone){ alert ('iphone');

but if I attempt to do this for Firefox / Mozilla, I can't get it to work. I've tried:

var isFirefox = navigator.userAgent.match(/Mozilla/i != null);

and

var isFirefox = navigator.userAgent.match(/Firefox/i != null);

I visited whatismyuseragent.com and got the following:

Mozilla/5.0 (Android;Linux armv7l; rv6.0) Gecko/20110811 Gecko Firefox/6.0 Fennec/6.0

Any idea how I properly detect this? I need to write some firefox specific code.

mheavers
  • 28,595
  • 55
  • 189
  • 291
  • 1
    Depending on what you're trying to do, you might consider doing feature detection instead (http://ejohn.org/blog/future-proofing-javascript-libraries/). Also see this other question on SO: http://stackoverflow.com/questions/1294586/browser-detection-versus-feature-detection – mbillard Aug 30 '11 at 16:02

5 Answers5

19

You can use the navigator.userAgent to detect the browser and navigator.platform to detect the current platform.

To Detect Firefox:

var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;

To Detect Android:

var is_android = navigator.platform.toLowerCase().indexOf("android") > -1;

To Detect Both:

if(is_firefox && is_android)
    //Do Work

I would recommend using something like modernizr to avoid browser detection and focus on feature detection.

Rion Williams
  • 72,294
  • 36
  • 192
  • 318
  • OP is asking to detect the mobile version. How will this differ between mobile and non-mobile? – JaredPar Aug 30 '11 at 16:00
  • Should be fixed - added platform detection in there as well. – Rion Williams Aug 30 '11 at 16:10
  • Sniffing for "firefox" is almost certainly the wrong thing to do. You probably want to look for "gecko/" instead. Sadly, looking for "gecko" doesn't work because of WebKit-based browsers often including that string in the UA string... – Boris Zbarsky Aug 30 '11 at 16:49
  • It's a pity Firefox Mobile doesn't support touch events, otherwise you could just do a simple ("ontouchstart" in window) and be done with it. Now you need to do browser sniffing and we're all back in the nineties again :( – Husky Nov 09 '11 at 13:54
  • 1
    Firefox now returns "Linux armv7l" as the platform instead of android, so this won't work. You can however check for "android" and "firefox" in the userAgent, which includes both, e.g: "Mozilla/5.0 (Android 7.0; Mobile; rv:50.0) Gecko/50.0 Firefox/50.0" – MarkLunney Nov 24 '16 at 12:23
4

var isFirefox = /Android.+Firefox\//.test(navigator.userAgent);

Nikita Gavrilov
  • 497
  • 7
  • 13
2

The mobile version of Firefox is Fennec, so just search for that:

var is_Firefox = navigator.userAgent.toLowerCase().indexOf('fennec') > -1;
mpw
  • 180
  • 3
  • 7
1

None of the above functions were working for me, specifically buriwoy was detecting either android or firefox, this version of his function works:

function detectAndroidFirefox () {
   var agent = navigator.userAgent.toLowerCase();
   if(agent.indexOf('firefox') >= 0){
     if(agent.indexOf("android") >= 0){
       return true;    
     } else{
       return false;
     }
   } else{
     return false;
   }
}
math0ne
  • 742
  • 5
  • 12
-1

Rion's answer doesn't work (at least anymore), because navigator.platform doesn't return Android, it returns Linux.

I wrote a function which seems to work:

function detectAndroidFirefox () {
   var agent = navigator.userAgent.toLowerCase();
   return (agent.indexOf('firefox') + agent.indexOf("android")) >= 0;
}

Thought maybe someone will need this.

chapani
  • 420
  • 4
  • 13