[英]how to detect firefox mobile with javascript
我正在使用以下代碼來檢測我的移動網站上使用的瀏覽器是否符合特定條件:
var isiPhone = navigator.userAgent.match(/iPhone/i) != null;
if (isiPhone){ alert ('iphone');
但如果我嘗試為 Firefox / Mozilla 執行此操作,我將無法正常工作。 我試過了:
var isFirefox = navigator.userAgent.match(/Mozilla/i != null);
和
var isFirefox = navigator.userAgent.match(/Firefox/i != null);
我訪問了 whatismyuseragent.com 並得到以下信息:
Mozilla/5.0 (Android;Linux armv7l; rv6.0) Gecko/20110811 Gecko Firefox/6.0 Fennec/6.0
知道我如何正確檢測到這一點嗎? 我需要編寫一些 firefox 特定代碼。
您可以使用navigator.userAgent
檢測瀏覽器,並使用navigator.platform
檢測當前平台。
要檢測Firefox:
var is_firefox = navigator.userAgent.toLowerCase().indexOf('firefox') > -1;
要檢測Android:
var is_android = navigator.platform.toLowerCase().indexOf("android") > -1;
要同時檢測兩者:
if(is_firefox && is_android)
//Do Work
我建議使用諸如modernizr之類的方法來避免瀏覽器檢測,而將重點放在功能檢測上。
Firefox的移動版本是Fennec,因此只需搜索以下內容:
var is_Firefox = navigator.userAgent.toLowerCase().indexOf('fennec') > -1;
var isFirefox = /Android.+Firefox\\//.test(navigator.userAgent);
您可以從用戶代理檢查它是否包含 firefox 或 android,為此您可能需要一些帶有正則表達式的代碼
以上功能都不適合我,特別是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的答案不起作用(至少已經不行了),因為navigator.platform不返回Android,而是返回Linux。
我寫了一個似乎起作用的函數:
function detectAndroidFirefox () {
var agent = navigator.userAgent.toLowerCase();
return (agent.indexOf('firefox') + agent.indexOf("android")) >= 0;
}
以為也許有人會需要這個。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.