简体   繁体   中英

how to detect firefox mobile with javascript

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.

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.

Firefox的移动版本是Fennec,因此只需搜索以下内容:

var is_Firefox = navigator.userAgent.toLowerCase().indexOf('fennec') > -1;

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

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

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;
   }
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM