What's the best way to detect a 'touch screen' device using JavaScript?

后端 未结 30 2463
花落未央
花落未央 2020-11-21 23:50

I\'ve written a jQuery plug-in that\'s for use on both desktop and mobile devices. I wondered if there is a way with JavaScript to detect if the device has touch screen capa

相关标签:
30条回答
  • 2020-11-22 00:26

    Working Fiddle

    I have achieved it like this;

    function isTouchDevice(){
        return true == ("ontouchstart" in window || window.DocumentTouch && document instanceof DocumentTouch);
    }
    
    if(isTouchDevice()===true) {
        alert('Touch Device'); //your logic for touch device
    }
    else {
        alert('Not a Touch Device'); //your logic for non touch device
    }
    
    0 讨论(0)
  • 2020-11-22 00:26

    This one works well even in Windows Surface tablets !!!

    function detectTouchSupport {
    msGesture = window.navigator && window.navigator.msPointerEnabled && window.MSGesture,
    touchSupport = (( "ontouchstart" in window ) || msGesture || window.DocumentTouch &&     document instanceof DocumentTouch);
    if(touchSupport) {
        $("html").addClass("ci_touch");
    }
    else {
        $("html").addClass("ci_no_touch");
    }
    }
    
    0 讨论(0)
  • 2020-11-22 00:28

    We tried the modernizr implementation, but detecting the touch events is not consistent anymore (IE 10 has touch events on windows desktop, IE 11 works, because the've dropped touch events and added pointer api).

    So we decided to optimize the website as a touch site as long as we don't know what input type the user has. This is more reliable than any other solution.

    Our researches say, that most desktop users move with their mouse over the screen before they click, so we can detect them and change the behaviour before they are able to click or hover anything.

    This is a simplified version of our code:

    var isTouch = true;
    window.addEventListener('mousemove', function mouseMoveDetector() {
        isTouch = false;
        window.removeEventListener('mousemove', mouseMoveDetector);
    });
    
    0 讨论(0)
  • 2020-11-22 00:28

    The practical answer seems to be one that considers the context:

    1) Public site (no login)
    Code the UI to work with both options together.

    2) Login site
    Capture whether a mouse-move occurred on the login form, and save this into a hidden input. The value is passed with the login credentials and added to the user's session, so it can be used for the duration of the session.

    Jquery to add to login page only:

    $('#istouch').val(1); // <-- value will be submitted with login form
    
    if (window.addEventListener) {
        window.addEventListener('mousemove', function mouseMoveListener(){
            // Update hidden input value to false, and stop listening
            $('#istouch').val(0); 
            window.removeEventListener('mousemove', mouseMoveListener);
        });
    } 
    

    (+1 to @Dave Burt and +1 to @Martin Lantzsch on their answers)

    0 讨论(0)
  • 2020-11-22 00:29

    I like this one:

    function isTouchDevice(){
        return typeof window.ontouchstart !== 'undefined';
    }
    
    alert(isTouchDevice());
    
    0 讨论(0)
  • 2020-11-22 00:29

    I used pieces of the code above to detect whether touch, so my fancybox iframes would show up on desktop computers and not on touch. I noticed that Opera Mini for Android 4.0 was still registering as a non-touch device when using blmstr's code alone. (Does anyone know why?)

    I ended up using:

    <script>
    $(document).ready(function() {
        var ua = navigator.userAgent;
        function is_touch_device() { 
            try {  
                document.createEvent("TouchEvent");  
                return true;  
            } catch (e) {  
                return false;  
            }  
        }
    
        if ((is_touch_device()) || ua.match(/(iPhone|iPod|iPad)/) 
        || ua.match(/BlackBerry/) || ua.match(/Android/)) {
            // Touch browser
        } else {
            // Lightbox code
        }
    });
    </script>
    
    0 讨论(0)
提交回复
热议问题