我做了一些研究,这个问题也出现了,但不是我想要的方式。我正在为客户端构建一个页面,这是一个QR码登陆,这是一个下载应用程序的地方。所以他不需要在页面上打印2个QR码,我想检测当前的操作系统(苹果/安卓/其他[不支持]),并根据该值修改我的元素。

我已经看过脚本“detectmobilebrowsers”,它的目的只是告诉用户是否移动,而我想弄清楚用户正在运行什么操作系统,并建议最好的应用程序版本。

我发现与这个问题类似的其他答案要么过时,要么不可靠(Android平板电脑浏览器没有检测),所以我正在寻找一些新的答案。我怎样才能做到这一点呢?(最好使用jQuery - Javascript - PHP的顺序)。


当前回答

这是一个简单的js脚本(在html标签),检测设备类型,然后重定向到正确的商店。

<script>
var userAgent = navigator.userAgent || navigator.vendor || window.opera;

    // Huawei Phone must come first because its UA also contains "Android"
    if (userAgent.includes("HUAWEI")) {
       window.location.replace("https://appgallery.huawei.com/app/C101184875");
    } else if (userAgent.includes("Android")) {
        window.location.replace("https://play.google.com/store/apps/details?id=org.telegram.messenger&hl=en&gl=US");
    } else if (userAgent.includes("iPhone")) {
        window.location.replace("https://apps.apple.com/us/app/telegram-messenger/id686449807");
    } else {
        //cant be sure about device, so redirect to home page
        window.location.replace("https://google.ro");
    }
</script>

其他回答

这是一个简单的js脚本(在html标签),检测设备类型,然后重定向到正确的商店。

<script>
var userAgent = navigator.userAgent || navigator.vendor || window.opera;

    // Huawei Phone must come first because its UA also contains "Android"
    if (userAgent.includes("HUAWEI")) {
       window.location.replace("https://appgallery.huawei.com/app/C101184875");
    } else if (userAgent.includes("Android")) {
        window.location.replace("https://play.google.com/store/apps/details?id=org.telegram.messenger&hl=en&gl=US");
    } else if (userAgent.includes("iPhone")) {
        window.location.replace("https://apps.apple.com/us/app/telegram-messenger/id686449807");
    } else {
        //cant be sure about device, so redirect to home page
        window.location.replace("https://google.ro");
    }
</script>

您还可以创建Firbase动态链接,这将根据您的需求工作。它支持多种平台。该链接可以手动创建,也可以通过编程创建。你可以把这个链接嵌入到二维码中。

如果目标应用程序已安装,该链接将重定向用户到应用程序。如果未安装,它将重定向到Play商店/应用程序商店/任何其他配置的网站。

可以使用导航器。获取安装浏览器的操作系统的平台。

function getPlatform() {
   var platform = ["Win32", "Android", "iOS"];

   for (var i = 0; i < platform.length; i++) {

       if (navigator.platform.indexOf(platform[i]) >- 1) {

           return platform[i];
       }
   }
}

getPlatform();

如果你的网站使用React Js,请使用https://www.npmjs.com/package/react-device-detect

function getOS() {
  var uA = navigator.userAgent || navigator.vendor || window.opera;
  if ((/iPad|iPhone|iPod/.test(uA) && !window.MSStream) || (uA.includes('Mac') && 'ontouchend' in document)) return 'iOS';

  var i, os = ['Windows', 'Android', 'Unix', 'Mac', 'Linux', 'BlackBerry'];
  for (i = 0; i < os.length; i++) if (new RegExp(os[i],'i').test(uA)) return os[i];
}

你现在可以这样使用它:

alert(getOS());

工作代码片段:

函数getOS() { var uA =导航器。userAgent || navigator。window.opera; if (/iPad|iPhone|iPod/.test(uA) && !window.MSStream) || (uA.includes('Mac') && 'ontouchend' in document))返回'iOS'; var, os =[“窗口”,“Android”,“Unix”,“苹果”,“Linux”,“黑莓”); For (i = 0;I < os.length;我+ +)如果(新RegExp (os(我),“我”)。test (uA))返回操作系统(我); } console.log (getOS ());