UNPKG

759 BJavaScriptView Raw
1/**
2 * 检测浏览器类型和版本
3 * 输出格式如下:
4 *
5 * "Chrome-67"
6 * "IE 11"
7 * "MSIE-9"
8 */
9const detectBrowser = function(ua) {
10 var tem;
11 var M =
12 ua.match(/(opera|chrome|safari|firefox|msie|trident(?=\/))\/?\s*(\d+)/i) ||
13 [];
14 if (/trident/i.test(M[1])) {
15 tem = /\brv[ :]+(\d+)/g.exec(ua) || [];
16 return "IE " + (tem[1] || "");
17 }
18 if (M[1] === "Chrome") {
19 tem = ua.match(/\b(OPR|Edge)\/(\d+)/);
20 if (tem != null)
21 return tem
22 .slice(1)
23 .join(" ")
24 .replace("OPR", "Opera");
25 }
26 M = M[2] ? [M[1], M[2]] : [navigator.appName, navigator.appVersion, "-?"];
27 if ((tem = ua.match(/version\/(\d+)/i)) != null) M.splice(1, 1, tem[1]);
28 return M.join("-");
29};
30
31export default detectBrowser;