JS获取手机型号和系统
程序员文章站
2022-07-09 21:38:57
废话不多说,直接上源码 ......
废话不多说,直接上源码
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>