【问题标题】:Detect if device is iOS or Android, and then redirect检测设备是iOS还是Android,然后重定向
【发布时间】:2019-01-25 09:50:19
【问题描述】:

所以我有一个网站,我想要的是当打开我的网站时:www.example.com/download.html 我想首先检测设备是iOS设备还是Android设备,然后重定向到另一个链接,例如 www.google.com(只是一个示例)。我想要不同操作系统的不同链接。关于如何管理这个的任何提示? :)

【问题讨论】:

标签: php html xml w3c


【解决方案1】:

您可以使用用户代理字符串来检测不同类型的设备,如下所示:

function androidOrIOS() {
    const userAgent = navigator.userAgent;
    if(/android/i.test(userAgent)){
        return 'android';
    }
    if(/iPad|iPhone|iPod/i.test(userAgent)){
        return 'ios';
    }
}

【讨论】:

  • 我要把这个放到download.html中吗?
  • 是的,您可以使用 window.location.replace("some link"); 重定向用户或 window.location.href = "一些链接";
  • 我是否需要在 html 文件中添加其他内容?如果代码是计算机而不是设备,您还可以显示代码吗?
【解决方案2】:

可以使用Navigator来判断设备类型

function navigate() {
    if((/Mobi|Android/i.test(navigator.userAgent))){
        window.location.href = 'android url ';
    }
    if(/Mobi|iPad|iPhone|iPod/i.test(navigator.userAgent)){
        window.location.href = 'ios url ';
    }
}

【讨论】:

  • Navigator 的支持范围有多广?
  • 在我看来,在第二个if 中搜索“Mobi”是不必要的,因为第一个已经捕获了这种情况。总的来说,在我看来,仅凭这一点就无法正常工作。
猜你喜欢
  • 2015-11-11
  • 1970-01-01
  • 2019-12-31
  • 1970-01-01
  • 2017-05-09
  • 2013-06-02
  • 1970-01-01
  • 1970-01-01
  • 2015-11-11
相关资源
最近更新 更多