JavaScript获取主流手机系统和型号
<script src="http://cdn.bootcss.com/jquery/3.3.1/jquery.min.js"></script> <script src="http://cdnjs.cloudflare.com/ajax/libs/mobile-detect/1.4.1/mobile-detect.min.js"></script> <script> //判断数组中是否包含某字符串 Array.prototype.contains = function(needle) { for (i in this) { if (this[i].indexOf(needle) > 0) return i; } return -1; } var device_type = navigator.userAgent;//获取userAgent信息 var md = new MobileDetect(device_type);//初始化mobile-detect var os = md.os();//获取系统 var model = ""; if (os == "iOS") {//ios系统的处理 os = md.os() + md.version("iPhone"); model = md.mobile(); } else if (os == "AndroidOS") {//Android系统的处理 os = md.os() + md.version("Android"); var sss = device_type.split(";"); var i = sss.contains("Build/"); if (i > -1) { model = sss[i].substring(0, sss[i].indexOf("Build/")); } } else { os = '非主流OS'; model = 'unKnown'; } document.write("操作系统:"+ os + "<br/>型号:" + model);//打印到页面 </script>
用手机打开页面运行结果如下: