检测设备是iOS还是Android,然后重定向 [英] Detect if device is iOS or Android, and then redirect
本文介绍了检测设备是iOS还是Android,然后重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
所以我有一个网站,我想要的是当打开我的网站:www.example.com/download.html 我想首先检测设备是iOS设备还是Android设备,然后重定向到另一个链接,例如 www.google.com(只是一个例子).我想要不同操作系统的不同链接.有关如何管理此问题的任何提示?:)
So I have a website, and what I want is that when the open my website: www.example.com/download.html I want to first detect if the device is iOS device or Android device, and then redirect to another link, for example www.google.com (just an example). I want different link for different OS. Any tips on how I can manage this? :)
推荐答案
您可以使用用户代理字符串来检测不同类型的设备,如下所示:
You can use the user agent string to detect different kinds of devices like so:
function androidOrIOS() {
const userAgent = navigator.userAgent;
if(/android/i.test(userAgent)){
return 'android';
}
if(/iPad|iPhone|iPod/i.test(userAgent)){
return 'ios';
}
}
这篇关于检测设备是iOS还是Android,然后重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文