我做了一些研究,这个问题已经出现了,但不是我想要的方式。我正在为一个客户端构建一个页面,它是一个二维码登陆页面,是一个下载应用程序的地方。所以他不必在一个页面上打印出2个二维码,我希望检测当前的操作系统(Apple/Android/Othernot支持),并根据该值修改我的元素。
我看过脚本"detectmobilebrowsers“,它的目的只是为了告诉用户是否是移动用户,而我想找出用户运行的是什么操作系统,并建议最好的应用程序版本。
我找到的其他类似这个问题的答案似乎要么过时了,要么不可靠(对Android平板电脑浏览器没有检测到),所以我在寻找新的东西。我如何才能做到这一点?(最好按照这个顺序使用jQuery - Javascript - PHP )。
发布于 2014-02-13 07:24:21
您可以测试用户代理字符串:
/**
* 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";
}
发布于 2014-02-13 09:08:17
这个问题已经在这里得到了解决:What is the best way to detect a mobile device in jQuery?。
根据公认的答案,他们基本上是测试它是iPhone、iPod、安卓设备还是任何返回true的设备。只需保留你想要的,例如,if( /Android/i.test(navigator.userAgent) ) { // some code.. }
将只为安卓用户代理返回true。
然而,用户代理并不是真正可靠的,因为它们可以改变。最好的办法仍然是开发出适用于所有移动平台的通用软件。
发布于 2019-01-30 10:25:46
如果您的网站使用React Js,请使用https://www.npmjs.com/package/react-device-detect
https://stackoverflow.com/questions/21741841
复制相似问题