检测设备是iOS还是Android,然后重定向



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

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

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

您可以使用导航器来确定设备的类型

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 ';
}
}

最新更新