廢話不多說,直接上源碼 ...
廢話不多說,直接上源碼
1 <!DOCTYPE html> 2 <html> 3 4 <head> 5 <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" /> 6 <meta name="viewport" content="width=device-width, initial-scale=1,maximum-scale=1,user-scalable=no"> 7 <title>JS獲取手機型號和系統</title> 8 </head> 9 10 <body> 11 <div id="app"></div> 12 </body> 13 <script src="https://cdn.bootcss.com/jquery/3.2.1/jquery.min.js"></script> 14 <script src="mobile-detect.min.js"></script> <!-- mobile-detect可以通過npm下載 --> 15 <script> 16 //判斷數組中是否包含某字元串 17 Array.prototype.contains = function(needle) { 18 for (i in this) { 19 if (this[i].indexOf(needle) > 0) 20 return i; 21 } 22 return -1; 23 } 24 25 var device_type = navigator.userAgent; //獲取userAgent信息 26 console.log(device_type); //列印到頁面 27 var md = new MobileDetect(device_type); //初始化mobile-detect 28 var os = md.os(); //獲取系統 29 var model = ""; 30 if (os == "iOS") { //ios系統的處理 31 os = md.os() + md.version("iPhone"); 32 model = md.mobile(); 33 } else if (os == "AndroidOS") { //Android系統的處理 34 os = md.os() + md.version("Android"); 35 var sss = device_type.split(";"); 36 var i = sss.contains("Build/"); 37 if (i > -1) { 38 model = sss[i].substring(0, sss[i].indexOf("Build/")); 39 } 40 } 41 console.log(os + "---" + model); //列印系統版本和手機型號 42 $('#app').html(device_type + '</br></br>' + os + "---" + model); 43 </script> 44 45 </html>