12

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.

Christophe Quintard
  • 1,858
  • 1
  • 22
  • 30
mheavers
  • 29,530
  • 58
  • 194
  • 315
  • 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

7 Answers7

21

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
  • 74,820
  • 37
  • 200
  • 327
  • 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
5
var isFirefox = /Android.+Firefox\//.test(navigator.userAgent);
Johann
  • 12,158
  • 11
  • 62
  • 89
Nikita Gavrilov
  • 527
  • 8
  • 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
  • 2
  • 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
  • 752
  • 6
  • 12
0

you can check from user agent if it's contain firefox or android, for this maybe you need some code with regex

  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Aug 27 '22 at 07:30
0

This detects Firefox on Android plain and simple:

isFxAndroid = /^Linux a/.test(navigator.oscpu);

Or the same thing more modern with startsWith:

isFxAndroid = navigator.oscpu.startsWith("Linux a");

How it works:
if (navigator.oscpu) { /* I'm Firefox */ }
is a well known way to detect Mozilla browsers (Firefox), other browsers return undefined.
According to the HTML spec, Gecko browsers (Firefox) must return a string for navigator.oscpu:
If the navigator compatibility mode is Gecko, then the user agent must also support the following partial interface
https://html.spec.whatwg.org/multipage/system-state.html#concept-navigator-compatibility-mode

Firefox on Android returns fornavigator.oscpu something like Linux armv71 or Linux aarch64.
Firefox on Linux Desktop always returns Linux x86_64; this string is frozen for many years (regardless of the actual CPU).
So you get no Desktop Firefox with the proposed query.

You can test your browser's return value for navigator.oscpu here:
http://jeka.info/test/navigator.html

j.j.
  • 1,914
  • 15
  • 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
  • 14