雖然現代前端提倡「特性檢測」而非「瀏覽器檢測」,但在統計分析、相容性降級、針對性 Polyfill 載入、安全風控等場景,UserAgent (UA) 解析仍不可或缺。本文系統整理 Windows、iOS、Android 三大平台主流瀏覽器 UA 特徵,提供解析邏輯與可維護的程式碼架構。
Mozilla/5.0 (Platform; Encryption; OS-or-CPU; Locale) Engine/Version (Browser/Version) ...
關鍵段落:
Mozilla/5.0:歷史遺留,幾乎所有瀏覽器開頭皆為此。(...):平台/作業系統資訊(Windows NT 10.0、iPhone OS、Android、Linux 等)。Engine/Version:渲染引擎 (AppleWebKit、Gecko、Presto、Trident)。Browser/Version:瀏覽器標識 (Chrome、Firefox、Safari、Edge、OPR、MQQBrowser 等)。| 瀏覽器 | UA 關鍵特徵 | 粗略判斷 | 嚴格判斷 |
|---|---|---|---|
| IE 11 | Trident/7.0; rv:11.0 | /Trident\/7\.0.*rv:11\.0/ | /MSIE|Trident/ + rv:11 |
| IE 10 | MSIE 10.0 | /MSIE 10\.0/ | /MSIE 10\.0/ |
| Edge (Legacy) | Edge/12.x | /Edge\/\d+/ | /Edge\/\d+/ |
| Edge (Chromium) | Edg/80.x | /Edg\/\d+/ | /Edg\/\d+/ (注意大寫 E) |
| Chrome | Chrome/xx.x + Safari/xxx | /Chrome\/\d+/ | /Chrome\/\d+/ + 無 Edg/OPR |
| Firefox | Firefox/xx.x + Gecko/ | /Firefox\/\d+/ | /Firefox\/\d+/ + Gecko |
| Opera (Chromium) | OPR/xx.x + Chrome/ | /OPR\/\d+/ | /OPR\/\d+/ |
| Safari (Windows 罕見) | Safari/xxx 無 Chrome | /Safari\/ + 無 Chrome | /Version\/\d+.*Safari/ |
| 瀏覽器 | UA 關鍵特徵 | 判斷邏輯 |
|---|---|---|
| Safari (iPhone) | iPhone + Mobile/ + Safari/ | /iPhone/ + /Mobile/ + /Safari/ + 無 CriOS/FxiOS |
| Chrome iOS (CriOS) | CriOS/xx.x | /CriOS\/\d+/ (所有 iOS 瀏覽器底層均為 WebKit) |
| Firefox iOS (FxiOS) | FxiOS/xx.x | /FxiOS\/\d+/ |
| iPad (iPadOS 13+) | iPad 或 Macintosh (請求桌面版) | /iPad/ 或 /Macintosh/ + /AppleWebKit/ |
| UIWebView/WKWebView (App 內嵌) | 無 Safari/ 關鍵字 | 檢測 !/Safari/ 且含 Mobile/ |
| 瀏覽器 | UA 關鍵特徵 | 判斷邏輯 |
|---|---|---|
| Chrome Android | Linux; Android + Chrome/xx.x + Mobile Safari/ | /Android/ + /Chrome\/\d+/ + 無 Edg/OPR |
| Firefox Android | Android + Firefox/xx.x + Gecko/ + Fennec/ | /Firefox\/\d+/ + /Android/ |
| Opera Mobile | Opera/ 或 OPR/ + Android | /OPR\/\d+/ 或 /Opera\/\d+/ |
| WebView (App 內嵌) | 含 wv 或 Version/ 無 Chrome | /Version\/\d+.*Chrome\/\d+/ (WebView) 或 無 Chrome |
| 三星/小米/華為 內建瀏覽器 | 含廠商關鍵字 MiuiBrowser/HuaweiBrowser 等 | 各廠商關鍵字匹配 |
function getOS(ua) {
ua = ua.toLowerCase();
if (/windows nt 10\.0/.test(ua)) return 'Windows 10';
if (/windows nt 6\.3/.test(ua)) return 'Windows 8.1';
if (/windows nt 6\.2/.test(ua)) return 'Windows 8';
if (/windows nt 6\.1/.test(ua)) return 'Windows 7';
if (/windows nt 6\.0/.test(ua)) return 'Windows Vista';
if (/windows nt 5\.1/.test(ua)) return 'Windows XP';
if (/mac os x (\d+[._]\d+)/.test(ua)) return 'macOS ' + RegExp.$1.replace('_', '.');
if (/iphone/.test(ua)) return 'iOS';
if (/ipad/.test(ua)) return 'iPadOS';
if (/android/.test(ua)) return 'Android';
if (/linux/.test(ua)) return 'Linux';
return 'Unknown';
}
function getEngine(ua) {
ua = ua.toLowerCase();
if (/trident/.test(ua)) return 'Trident (IE)';
if (/edgehtml/.test(ua)) return 'EdgeHTML (Legacy Edge)';
if (/edg\//.test(ua)) return 'Blink (Chromium Edge)';
if (/opr\//.test(ua)) return 'Blink (Opera)';
if (/chrome\//.test(ua)) return 'Blink (Chrome)';
if (/firefox\//.test(ua)) return 'Gecko (Firefox)';
if (/applewebkit\//.test(ua)) return 'WebKit (Safari/其他)';
return 'Unknown';
}
const UAParser = (function () {
const parsers = [
// 瀏覽器解析器 (優先序越高越先匹配)
{ name: 'Edge', test: ua => /Edg\/(\d+)/.exec(ua), engine: 'Blink' },
{ name: 'Chrome', test: ua => /Chrome\/(\d+)/.exec(ua), engine: 'Blink' },
{ name: 'Firefox', test: ua => /Firefox\/(\d+)/.exec(ua), engine: 'Gecko' },
{ name: 'Safari', test: ua => /Version\/(\d+).*Safari/.exec(ua), engine: 'WebKit' },
{ name: 'Opera', test: ua => /OPR\/(\d+)/.exec(ua), engine: 'Blink' },
{ name: 'IE', test: ua => /(?:MSIE |rv:)(\d+)/.exec(ua), engine: 'Trident' },
];
const osParsers = [
{ name: 'Windows 10', test: ua => /Windows NT 10\.0/.test(ua) },
{ name: 'Windows 7', test: ua => /Windows NT 6\.1/.test(ua) },
{ name: 'macOS', test: ua => /Mac OS X (\d+[._]\d+)/.exec(ua) },
{ name: 'iOS', test: ua => /iPhone|iPad|iPod/.test(ua) },
{ name: 'Android', test: ua => /Android/.test(ua) },
{ name: 'Linux', test: ua => /Linux/.test(ua) },
];
function parse(uaString) {
uaString = uaString || navigator.userAgent;
const ua = uaString.toLowerCase();
// 瀏覽器
let browser = { name: 'Unknown', version: '0', engine: 'Unknown' };
for (const p of parsers) {
const match = p.test(uaString);
if (match) {
browser = { name: p.name, version: match[1] || '0', engine: p.engine };
break;
}
}
// OS
let os = { name: 'Unknown', version: '0' };
for (const p of osParsers) {
const match = p.test(uaString);
if (match) {
os = { name: p.name, version: match[1] || '0' };
break;
}
}
return { ua: uaString, browser, os };
}
return { parse };
})();
// 使用
const info = UAParser.parse();
console.log('瀏覽器:', info.browser.name, info.browser.version);
console.log('引擎:', info.browser.engine);
console.log('作業系統:', info.os.name, info.os.version);
// ❌ 壞例:UA 判斷決定是否用 fetch
if (/Chrome\/40/.test(navigator.userAgent)) { useFetch(); }
// ✅ 好例:特性檢測
if (window.fetch) { useFetch(); }
else { loadPolyfillThenUseFetch(); }
// ✅ 好例:CSS @supports
@supports (display: grid) { .layout { display: grid; } }
// ✅ 好例:條件載入 Polyfill
if (!Element.prototype.closest) {
import('element-closest-polyfill');
}
ua-parser-js):輕量、支援 Node/瀏覽器、正則規則定期更新。掌握 UA 解析原理與現代替代方案,既能應對舊系統相容需求,又能在新專案遵循最佳實踐,避免 UA 檢測濫用帶來的維護噩夢。