js 判断设备及安卓设备判断不准确问题

news/2024/7/20 20:20:57 标签: android, iOS, userAgent, navigator, 判断设备
<!-- web 获取 设备类型.注意: 一旦app端设置了userAgent 将会覆盖设置自带的ua -->
  <script>
    var agent = "";
    function isAndroid() {
      agent = navigator.userAgent.toLowerCase();
      let result = {
        device: function () {
          if (/windows/.test(agent)) {
            return 'windows pc';
          } else if (/iphone|ipod/.test(agent) && /mobile/.test(agent)) {
            return 'iphone';
          } else if (/ipad/.test(agent) && /mobile/.test(agent)) {
            return 'ipad';
          } else if (/android/.test(agent) && /mobile/.test(agent)) {
            return 'android';
          } else if (/linux/.test(agent)) {
            return 'linux pc';
          } else if (/mac/.test(agent)) {
            return 'mac';
          } else {
            return 'other';
          }
        }(),
      };
      document.getElementById("agent").innerText = agent;
      document.getElementById("showAndroid").innerText = result.device;
      return result.device;
    }
  </script>


mozilla/5.0 (macintosh; intel mac os x 10_15_6) applewebkit/537.36 (khtml, like gecko) chrome/99.0.4844.51 safari/537.36

不准确的问题原因

当安卓或则iOS设备, 在其WebView组件里面设置了userAgent, 将覆盖掉原设备的userAgent, 会导致原来的判断代码失效


http://www.niftyadmin.cn/n/1557041.html

相关文章

正则表达式/不用转义,\需要转义

----------- ^\\d{4}\\d{2}\\d{2}$ java两种方式 String.matcher("reg") 或Pattern.match("reg",source)转载于:https://www.cnblogs.com/fpcbk/p/9984396.html

Vue中使用axios的几个注意点

在项目中&#xff0c;经常会使用axios进行数据的请求。但是在vue使用有几点注意的事项&#xff0c;以下是基本的几个条例。 1.首先在vue 项目中下载 axios 包 npm install axios -S2.在 main.js 中引入 axios import axios from axios3.注册axios Vue.prototype.$axios axios;…

RN-Android 封装原生安卓web组件 - H5白屏/卡顿/渲染问题

其实还是该问题的后续BUG 解决完安卓端微信支付问题后, 发现部分H5页面加载异常, 出现卡顿/半透明层遮盖效果异常/页面渲染异常/白屏等问题 排查: 1.webSetting的设置? 经过确认,暂时无法从webSetting 层面解决 2.硬件加速/软件加速? 经过实验,设置wv.setLayerType(Vie…

(8)添加Vuex支持

2019独角兽企业重金招聘Python工程师标准>>> #&#xff08;8&#xff09;添加Vuex支持 1 安装 cnpm install --save vuex2 引入 2.1 /src/store/index.js 新建/src/store/目录&#xff0c;用于存放Vuex模块内容 2.1.1 模块化 由于使用单一状态树&#xff0c;应用的所…

【Redis 】redis真正企业中的应用场景,大佬来讨论一下呗

话不多说开始重点 分布式锁&#xff1a;使用 Redis 的原子操作和过期时间特性&#xff0c;可以实现分布式锁&#xff0c;用于保证多个进程或线程对共享资源的互斥访问。分布式锁在分布式系统中起到了重要的作用&#xff0c;确保了数据的一致性和并发控制。 消息队列与任务队列…

react-navigation 4.x navigationOptions 设置导航栏透明

static navigationOptions ({navigation}: any) > { return {headerShown: true,// 是否使用导航栏headerTransparent:true, //导航栏是否透明} }

RN-升级XCODE13.3.1 编译 _initializeModules:(NSArray<id<RCTBridgeModule>> *)modules 报错

2022_05_05: 由于必须上传App Store必须要求xcode升级, 结果升级后编译报错 Cannot initialize a parameter of type NSArray<id<RCTBridgeModule>> * with an rvalue of type NSArray<Class> *这种大概两三个同类型的错误 解决方案在此: 目前采用第一种方案…

H5移动端实现手机震动效果

判断兼容 浏览器对振动API的支持情况&#xff0c;一个好的习惯就是在使用之前要检查一下当前你的应用环境、浏览器是否支持振动API。下面就是检测的方法&#xff1a; setTimeout(()>{navigator.vibrate navigator.vibrate || navigator.webkitVibrate || navigator.mozVi…