我做了一些研究,提出了这个问题,但不是我想要的方式。我正在为一个二维码登陆的客户端构建一个页面,这是一个下载应用程序的地方。所以他不必在一个页面上打印 2 个二维码,我想检测当前的操作系统(Apple/Android/Other[不支持])并根据该值修改我的元素。
我查看了脚本“detectmobilebrowsers”,该脚本旨在判断用户是否是移动设备,而我想弄清楚用户正在运行什么操作系统并建议最佳应用程序版本.
我发现与此问题类似的其他答案似乎已过时或不可靠(无法检测到 Android 平板电脑浏览器),因此我正在寻找新的东西。我怎样才能做到这一点? (最好按顺序使用 jQuery - Javascript - PHP)。
最佳答案
您可以测试用户代理字符串:
/**
* Determine the mobile operating system.
* This function returns one of 'iOS', 'Android', 'Windows Phone', or 'unknown'.
*
* @returns {String}
*/
function getMobileOperatingSystem() {
var userAgent = navigator.userAgent || navigator.vendor || window.opera;
// Windows Phone must come first because its UA also contains "Android"
if (/windows phone/i.test(userAgent)) {
return "Windows Phone";
}
if (/android/i.test(userAgent)) {
return "Android";
}
// iOS detection from: http://stackoverflow.com/a/9039885/177710
if (/iPad|iPhone|iPod/.test(userAgent) && !window.MSStream) {
return "iOS";
}
return "unknown";
}
关于javascript - 检测 iOS/Android 操作系统,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21741841/