js 获取 手机操作系统和手机品牌

需要引入
<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> class GetPhone{
     // 获取手机操作系统 static osType(){ const device_type = navigator.userAgent;
         // 对数据进行处理 const md = new MobileDetect(device_type); const os = md.os(); return os }; // 获取安卓品牌 static androidPhone(){ const device_type = navigator.userAgent;
          // 获取手机品牌 const result_list = device_type.split(";"); let index = 0 for (var i = 0; i < result_list.length; i++){ if (result_list[i].indexOf("Build/") > -1){ index = i break } } const model = result_list[index].substring(0, result_list[index].indexOf("Build/")); return model }
    // 获取苹果品牌
    static iosPhone(){
const device_type = navigator.userAgent;
const md = new MobileDetect(device_type);
const os = md.os() + md.version("iPhone");
const model = md.mobile();
return model
}
}
</script>

在这里有些情况下不太准,例如:华为在上边的哪个位置是 系列名,所以实际情况要判断下。  

 

参考: https://www.cnblogs.com/justphp/p/8439258.html

posted @ 2021-02-03 11:54  ShanCe-刘勇  阅读(1548)  评论(0编辑  收藏  举报