我被要求创建一个实际的HTML页面/JavaScript,以模拟使用JavaScript代码检测移动设备(iPhone/iPad/Android)的过程。然后,它将带用户进入另一个屏幕,要求他们输入电子邮件地址。
我被要求创建一个实际的HTML页面/JavaScript,以模拟使用JavaScript代码检测移动设备(iPhone/iPad/Android)的过程。然后,它将带用户进入另一个屏幕,要求他们输入电子邮件地址。
我建议您查看http://wurfl.io/
简单来说,如果您导入一个小的JS文件:
<script type='text/javascript' src="//wurfl.io/wurfl.js"></script>
{
"complete_device_name":"Google Nexus 7",
"is_mobile":true,
"form_factor":"Tablet"
}
WURFL.complete_device_name
$.browser
并改用$.support
)。function is_touch_device() {
return 'ontouchstart' in window // works on most browsers
|| 'onmsgesturechange' in window; // works on ie10
}
if (navigator.userAgentData.mobile) { // do something }
尽管Safari尚不支持该功能,
更多信息:https://developer.mozilla.org/en-US/docs/Web/API/NavigatorUAData/mobile
let isMouseAvailable = window.matchMedia("(any-pointer:coarse)").matches;
https://patrickhlauke.github.io/touch/touchscreen-detection/
自2015年以来,支持媒体匹配(Media matches),您可以在此处检查兼容性: https://caniuse.com/#search=matchMedia 简而言之,您应该维护与屏幕是否为触摸屏以及屏幕尺寸相关的变量。理论上,我可能会在鼠标操作的桌面上使用小屏幕。(hover)
吗? - SpoonMeiser这是一个关于如何检查网页加载在桌面端还是移动应用程序中的示例。
当页面加载时,JS将执行,您可以在页面加载时进行桌面端特定的操作,例如隐藏条形码扫描器。
<!DOCTYPE html>
<html>
<head>
<script type="text/javascript">
/*
* Hide Scan button if Page is loaded in Desktop Browser
*/
function hideScanButtonForDesktop() {
if (!(/Android|webOS|iPhone|iPad|iPod|BlackBerry|BB|PlayBook|IEMobile|Windows Phone|Kindle|Silk|Opera Mini/i.test(navigator.userAgent))) {
// Hide scan button for Desktop
document.getElementById('btnLinkModelScan').style.display = "none";
}
}
//toggle scanButton for Desktop on page load
window.onload = hideScanButtonForDesktop;
</script>
</head>
// var userAgent = navigator.userAgent.toLowerCase(); // this would actually get the user agent
var userAgent = "iphone"; /* Simulates User Agent for iPhone */
if (userAgent.indexOf('iphone') != -1) {
// some code here
}
!!~userAgent.indexOf('iphone') // >> true|false
- Cody<script src="mobile-detect.js"></script>
<script>
var md = new MobileDetect(window.navigator.userAgent);
// ... see below
</script>
var MobileDetect = require('mobile-detect'),
md = new MobileDetect(req.headers['user-agent']);
// ... see below
var md = new MobileDetect(
'Mozilla/5.0 (Linux; U; Android 4.0.3; en-in; SonyEricssonMT11i' +
' Build/4.1.A.0.562) AppleWebKit/534.30 (KHTML, like Gecko)' +
' Version/4.0 Mobile Safari/534.30');
// more typically we would instantiate with 'window.navigator.userAgent'
// as user-agent; this string literal is only for better understanding
console.log( md.mobile() ); // 'Sony'
console.log( md.phone() ); // 'Sony'
console.log( md.tablet() ); // null
console.log( md.userAgent() ); // 'Safari'
console.log( md.os() ); // 'AndroidOS'
console.log( md.is('iPhone') ); // false
console.log( md.is('bot') ); // false
console.log( md.version('Webkit') ); // 534.3
console.log( md.versionStr('Build') ); // '4.1.A.0.562'
console.log( md.match('playstation|xbox') ); // false
这是我的版本,与上面的版本相似,但我认为很适合参考。
if (mob_url == "") {
lt_url = desk_url;
} else if ((useragent.indexOf("iPhone") != -1 || useragent.indexOf("Android") != -1 || useragent.indexOf("Blackberry") != -1 || useragent.indexOf("Mobile") != -1) && useragent.indexOf("iPad") == -1 && mob_url != "") {
lt_url = mob_url;
} else {
lt_url = desk_url;
}
只需使用设备检测
deviceDetection.deviceType // phone | tablet according to device
由于我(在某种程度上没有成功地)寻找适当的解决方案来进行我的黑客,但无论如何,我希望在这里添加我的黑客:我简单地检查设备方向的支持,这似乎是移动设备和桌面之间最重要的区别:
var is_handheld = 0; //只是一个全局变量 if(window.DeviceOrientationEvent){is_handheld = 1;}
话虽如此,我个人认为页面还应该提供移动/桌面布局之间的手动选择。我有1920*1080的分辨率,可以对其进行缩放 - 过于简化和功能减少的WordPressoid组件并不总是一件好事。特别是基于不起作用的设备检测来强制使用布局 - 这种情况时常发生。