Search code examples
javascriptgoogle-chrometouchgoogle-chrome-devtoolsemulation

Chrome (Desktop) touch emulation not working


I wrote a little javascript and tried debugging it on latest stable Chrome for Windows 10 (as of writing v48)..

The script works perfectly with mouse-input but as soon as I emulate a mobile/touchdevice with touch emulation on - it's not working and the appropriate console.logs tell me that no touch was detected...

function hasTouch() {
        return 'ontouchstart' in document.documentElement;
}
var event_start = hasTouch() ? 'touchstart' : 'mousedown',
    event_move = hasTouch() ? 'touchmove' : 'mousemove',
    event_end = hasTouch() ? 'touchend' : 'mouseup';
console.log(event_start + "|" + event_move + "|" + event_end);

Firefox with touch emulation on works perfectly! Also physical touch devices work perfectly..

https://jsfiddle.net/j8kLz6wm/1/

So what is wrong with Chrome?


Solution

  • You should rewrite your hasTouch function like this:

    function hasTouch() {
      return 'ontouchstart' in window;
    }