[英]Check touch device using javascript
I have a small function that detects if my user is on a touch device that works perfectly... 我有一个小功能,可以检测用户是否在运行良好的触摸设备上...
function is_touch_device() {
return (('ontouchstart' in window) || (navigator.MaxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0));
}
if(!is_touch_device()) {
window.location.href = 'index.html';
}
I want to add a condition that if the device is an ipad, then don't carry out the redirect. 我要添加一个条件,如果设备是ipad,则不要执行重定向。
I've tried... 我试过了...
function is_touch_device() {
var isiPad = navigator.userAgent.match(/iPad/i) != null;
if( !isiPad ){
return (('ontouchstart' in window)
|| (navigator.MaxTouchPoints > 0)
|| (navigator.msMaxTouchPoints > 0));
}
};
if(!is_touch_device()) {
window.location.href = 'index.html';
}
But it stops touch devices redirecting, all of them in fact, any ideas? 但是它停止了触摸设备的重定向,实际上所有这些都有什么想法吗?
No need a function and try this script; 不需要功能,请尝试使用此脚本;
if( /Android|webOS|iPhone|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent) ) {
alert('Mobile device, exclude iPad');
}
else {
alert('Desktop');
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.