[英]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.但如果我尝试为 Firefox / Mozilla 执行此操作,我将无法正常工作。 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:我访问了 whatismyuseragent.com 并得到以下信息:
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.我需要编写一些 firefox 特定代码。
You can use the navigator.userAgent
to detect the browser and navigator.platform
to detect the current platform. 您可以使用navigator.userAgent
检测浏览器,并使用navigator.platform
检测当前平台。
To Detect Firefox: 要检测Firefox:
var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
To Detect Android: 要检测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. 我建议使用诸如modernizr之类的方法来避免浏览器检测,而将重点放在功能检测上。
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您可以从用户代理检查它是否包含 firefox 或 android,为此您可能需要一些带有正则表达式的代码
None of the above functions were working for me, specifically buriwoy was detecting either android or firefox, this version of his function works: 以上功能都不适合我,特别是buriwoy正在检测android或firefox,此功能的此版本有效:
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. Rion的答案不起作用(至少已经不行了),因为navigator.platform不返回Android,而是返回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. 以为也许有人会需要这个。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.