You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

JS浏览器操作系统检测代码在Windows、Android端识别错误求助

问题根因
  • 所有判定使用独立if而非互斥的else if,后续判定会直接覆盖之前的匹配结果,比如设备UA同时包含Win和Mac字段时,最终系统类型会被后面的Mac判定覆盖
  • 判定顺序完全错误:未优先匹配更细分的系统。安卓UA本身默认携带Linux字段,iOS UA也包含Mac相关字段,若先判定Linux、MacOS,会直接覆盖后续的移动端系统匹配结果
  • 存在低级拼写错误:Android被错写为Andoid,导致安卓的核心匹配逻辑完全失效
  • 规则配置错误:将MacOS专属的MacIntel、MacPPC等芯片标识错误归类到iOS,会导致Intel芯片的Mac设备直接被误识别为iOS
修复方案

优先匹配细分移动端系统,再匹配桌面端系统,所有判定使用else if互斥,删除无效规则,修正拼写错误,同时更换为兼容性更好的navigator.userAgent进行匹配:

console.log("werkt")
jQuery('.LeButton').click(function() {
  console.log(".LeButton")
  var OSName = "Unknown OS";
  const userAgent = navigator.userAgent;
  console.log(userAgent);
  // 优先匹配移动端系统
  if (userAgent.indexOf("iPhone") != -1 || userAgent.indexOf("iPod") != -1 || userAgent.indexOf("iPad") != -1 || userAgent.indexOf("iPhone Simulator") != -1) {
    OSName = 'Ios';
  } else if (userAgent.indexOf("Android") != -1) {
    OSName = 'Android';
  // 再匹配桌面端系统
  } else if (userAgent.indexOf("Win") != -1) {
    OSName = 'Windows';
  } else if (userAgent.indexOf("Mac") != -1) {
    OSName = 'MacOs';
  } else if (userAgent.indexOf("X11") != -1) {
    OSName = 'UNIX';
  } else if (userAgent.indexOf("Linux") != -1) {
    OSName = 'Linux';
  }
  // 跳转逻辑
  if (OSName == "MacOs") {
    window.location = 'https://get.anydesk.com/algsS7Tu/Develto_AnyDesk.dmg';
  } else if (OSName == "Windows") {
    window.location = 'https://get.anydesk.com/rWkVpCDA/Develto_AnyDesk.exe';
  } else if (OSName == "Linux") {
    window.location = 'https://get.anydesk.com/FAYb0MAc/Develto_AnyDesk.tar.gz';
  } else if (OSName == "Android") {
    window.location = 'https://get.anydesk.com/7LUtiVEA/AnyDesk.apk';
  } else if (OSName == "Ios") {
    window.location = 'https://itunes.apple.com/us/app/anydesk/id1176131273';
  }
  console.log(OSName)
});

HTML部分无需修改:

<script src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.6.0/jquery.min.js"></script>
<button class="LeButton">Go to download page </button>

内容的提问来源于stack exchange,提问作者user16814255

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.10.04 18:57:03