我正在寻找一个函数,返回一个布尔值,如果用户正在使用移动浏览器与否。
我知道我可以使用导航器。userAgent并使用regex编写该函数,但是用户代理对于不同的平台来说太复杂了。我怀疑匹配所有可能的设备是否容易,我认为这个问题已经解决了很多次,所以应该有某种完整的解决方案来完成这样的任务。
我正在看这个网站,但不幸的是,脚本是如此神秘,我不知道如何使用它为我的目的,这是创建一个返回true/false的函数。
我正在寻找一个函数,返回一个布尔值,如果用户正在使用移动浏览器与否。
我知道我可以使用导航器。userAgent并使用regex编写该函数,但是用户代理对于不同的平台来说太复杂了。我怀疑匹配所有可能的设备是否容易,我认为这个问题已经解决了很多次,所以应该有某种完整的解决方案来完成这样的任务。
我正在看这个网站,但不幸的是,脚本是如此神秘,我不知道如何使用它为我的目的,这是创建一个返回true/false的函数。
当前回答
我认为这才是真正的保护方案。
测试在Chrome和Firefox,移动和桌面。
var ___isMobileDevice;
const isMobileDeviceCheck = () => {
const mobileOsRegExp = "(Android|webOS|iPhone|iPod)";
if(screen.width < 500 || navigator.userAgent.match('/'+mobileOsRegExp+'/i')) {
___isMobileDevice = true;
}
if (___isMobileDevice) {
if (typeof window.orientation === "undefined") {
___isMobileDevice = false;
}
}
if (typeof navigator.userAgentData != "undefined" && !navigator.userAgentData.mobile) {
___isMobileDevice = false;
}
if ( typeof window.orientation !== "undefined" && ___isMobileDevice ) {
if (window.navigator.maxTouchPoints > 1 && (navigator.userAgentData.mobile || localStorage.mobile || 'ontouchstart' in document)) {
// mobile device found
console.log('Is mobile device!');
}
}
}
window.onresize = () => {
isMobileDeviceCheck();
}
isMobileDeviceCheck();
PS:作为浏览器扩展的Useragent切换器不能用这段代码欺骗你。
其他回答
以下是我对这个问题的重新思考的解决方案。仍然不完美。唯一真正的解决方案是设备制造商开始认真对待“移动”和“平板”用户代理字符串。
window.onload = userAgentDetect;
function userAgentDetect() {
if(window.navigator.userAgent.match(/Mobile/i)
|| window.navigator.userAgent.match(/iPhone/i)
|| window.navigator.userAgent.match(/iPod/i)
|| window.navigator.userAgent.match(/IEMobile/i)
|| window.navigator.userAgent.match(/Windows Phone/i)
|| window.navigator.userAgent.match(/Android/i)
|| window.navigator.userAgent.match(/BlackBerry/i)
|| window.navigator.userAgent.match(/webOS/i)) {
document.body.className += ' mobile';
alert('True - Mobile - ' + navigator.userAgent);
} else {
alert('False - Mobile - ' + navigator.userAgent);
}
if(window.navigator.userAgent.match(/Tablet/i)
|| window.navigator.userAgent.match(/iPad/i)
|| window.navigator.userAgent.match(/Nexus 7/i)
|| window.navigator.userAgent.match(/Nexus 10/i)
|| window.navigator.userAgent.match(/KFAPWI/i)) {
document.body.className -= ' mobile';
document.body.className += ' tablet';
alert('True - Tablet - ' + navigator.userAgent);
} else {
alert('False - Tablet - ' + navigator.userAgent);
}
}
当Nexus 7平板电脑只有Android UA字符串时会发生什么?首先,Mobile变成true,之后Tablet也变成true,但是Tablet会从body标签中删除Mobile UA字符串。
CSS:
body.tablet { background-color: green; }
body.mobile { background-color: red; }
为开发添加了警报线。Chrome控制台可以模拟许多手持设备。测试。
编辑:
不要使用这种方法,而是使用特征检测。市场上有如此多的设备和品牌,瞄准一个品牌永远不是正确的解决方案。
如何:
if (typeof screen.orientation !== 'undefined') { ... }
...因为智能手机通常支持这个属性,而桌面浏览器不支持。在MDN中见。
编辑1:正如@Gajus指出的,窗口。Orientation现在已弃用,不应该使用。
编辑2:您可以使用实验屏幕。Orientation而不是弃用的window.orientation。在MDN中见。
编辑3:从窗口更改。朝向屏幕。朝向
这是我在任何情况下发现的最好的工作方法。
const deviceMotionAvailable = Array.isArray(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/iPhone/i))
这取决于用例。所有的移动设备都需要电池。如果你想要的是在不耗尽电池的情况下计算电量,使用电池状态API:
navigator.getBattery().then(battery => {
battery.charging ? 'charging' : 'not charging';
});
如果您正在寻找的是表示的使用matchMedia,它返回一个布尔值:
if (window.matchMedia("(min-width: 400px)").matches) {
/* the viewport is at least 400 pixels wide */
} else {
/* the viewport is less than 400 pixels wide */
}
或者将它们结合起来,在平板设备上获得更好的用户体验。
使用window.screen怎么样?宽度”?
if (window.screen.width < 800) {
// do something
}
or
if($(window).width() < 800) {
//do something
}
我想这是最好的方法,因为每天都有新的移动设备!
(虽然我认为旧的浏览器不支持它,但试试看吧:))