简体   繁体   English

使用javascript检查触摸设备

[英]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.

相关问题 使用Java检查触摸设备 - Check for Touch device with Javascript 如何检查用户是否使用javascript触摸设备(智能手机) - how to check if user touch the device (the smartphone) with javascript 检查触摸设备上的scrollTop - Check scrollTop on touch device 使用 JavaScript 检测“触摸屏”设备的最佳方法是什么? - What's the best way to detect a 'touch screen' device using JavaScript? 使用javascript检测是否使用ie11触摸设备 - Detect if touch device with ie11 using javascript 是否有可能知道在使用javascript的具有触摸功能的设备上是否使用了真正的鼠标? - Is it possible to know if a real mouse is used on a device with touch features using javascript? 如何使用javascript检测触摸设备浏览器与桌面? - How to detect touch device browsers vs desktop using javascript? 检查音频和视频设备是否使用 javascript 连接 - check if audio and video device is connected using javascript 如何检测用户是否使用canvas和javascript在触摸设备上绘制了一个圆圈? - How to detect if a user has drawn a circle on a touch device using canvas and javascript? 如何使用 typescript 或 javascript 检查客户端的设备是否为平板电脑? - How to check if a client's device is a tablet, using typescript or javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM