【前端】JavaScript判断ua

判断设备是移动端还是PC

javascript 复制代码
isMobile() {
  // Regular expression to match common mobile user agent strings
  var mobileRegex = /Mobi|Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i;
  return mobileRegex.test(navigator.userAgent);
}

判断系统是IOS还是Android

javascript 复制代码
detectOS() {
  this.loading = true;
  var userAgent = navigator.userAgent || navigator.vendor || window.opera;

  // iOS
  if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
    this.device = "iOS";
  }
    // Android
  else if (userAgent.match(/Android/i)) {
    this.device = "Android";
  }
    // 如果不是上述两者,可能是PC或其他平台,可根据需要添加更多判断
  else {
    this.device = "Unknown";
  }
}
相关推荐
赵大仁12 分钟前
React Native 与 Expo
javascript·react native·react.js
于壮士hoho26 分钟前
Python | Dashboard制作
开发语言·python
程序员与背包客_CoderZ1 小时前
Node.js异步编程——Callback回调函数实现
前端·javascript·node.js·web
Asus.Blogs1 小时前
为什么go语言中返回的指针类型,不需要用*取值(解引用),就可以直接赋值呢?
开发语言·后端·golang
青瓦梦滋1 小时前
【语法】C++的多态
开发语言·c++
C_V_Better2 小时前
Java Spring Boot 控制器中处理用户数据详解
java·开发语言·spring boot·后端·spring
非凡ghost2 小时前
Pale Moon:速度优化的Firefox定制浏览器
前端·firefox
t198751282 小时前
基于Qt的OSG三维建模
java·开发语言
清灵xmf2 小时前
从 Set、Map 到 WeakSet、WeakMap 的进阶之旅
前端·javascript·set·map·weakset·weakmap
AI视觉网奇2 小时前
3d关键点 可视化
开发语言·python·pygame