javascript 如何使用javascript检测firefox mobile
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7246091/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to detect firefox mobile with javascript
提问by mheavers
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:
但是如果我尝试为 Firefox / Mozilla 执行此操作,则无法使其正常工作。我试过了:
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 的代码。
回答by Rion Williams
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:
检测安卓:
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 modernizrto avoid browser detection and focus on feature detection.
我建议使用诸如modernizr 之类的东西来避免浏览器检测并专注于特征检测。
回答by mpw
The mobile version of Firefox is Fennec, so just search for that:
Firefox 的移动版本是 Fennec,所以只需搜索:
var is_Firefox = navigator.userAgent.toLowerCase().indexOf('fennec') > -1;
回答by Nikita Gavrilov
var isFirefox = /Android.+Firefox\//.test(navigator.userAgent);
var isFirefox = /Android.+Firefox\//.test(navigator.userAgent);
回答by math0ne
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;
}
}
回答by chapani
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.
想也许有人会需要这个。