Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I detect device touch support in JavaScript?

In the past, when detecting whether a device supports touch events in JavaScript, we could do something like this:

var touch_capable = ('ontouchstart' in document.documentElement);

However, Google Chrome (17.x.x+) returns true for the above check, even if the underlying device does not support touch events. For example, running the above code on Windows 7 returns true, and thus if we combine it with something like:

var start_evt = (touch_capable) ? 'ontouchstart' : 'onmousedown';

On Google Chrome, the event is never fired since we're binding to ontouchstart. In short, does anyone know a reliable way to circumvent this? I am currently running the following check:

var touch_capable = ('ontouchstart' in document.documentElement && navigator.userAgent.toLowerCase().indexOf('chrome') == -1)

Which is far from ideal...

like image 488
BenM Avatar asked Jan 21 '13 13:01

BenM


People also ask

Why do you need a JavaScript library for touch screen detection?

You may need this detection while introducing newer smarter controls for touch screen users in the game app or a GPS and navigation application. While there are many JavaScript libraries such as Modernizer, jQuery, etc, that are explicitly designed to do such kind of tasks.

Is there a JavaScript library for Ultrabooks with touch screen?

While there are many JavaScript libraries such as Modernizer, jQuery, etc, that are explicitly designed to do such kind of tasks. It is noted that the device supports touch events doesn’t necessarily mean that it is exclusively a touch screen device. Many of the high-end ultrabooks are touch enabled.

Do we want to detect device touch or human touch?

We want to detect human touch, not device touch. I reckon that’s important enough to be repeated in a big font. We want to detect human touch, not device touch.


Video Answer


2 Answers

The correct answer is to handle both event types - they're not mutually exclusive.

For a more reliable test for touch support, also look for window.DocumentTouch && document instanceof DocumentTouch which is one of the tests used by Modernizr

Better yet, just use Modernizr yourself and have it do the feature detection for you.

Note though that you cannot prevent false positives, hence my first line above - you've got to support both.

like image 134
Alnitak Avatar answered Sep 19 '22 14:09

Alnitak


This is a modification of how Modernizr performs touch detection, with added support to work with IE10+ touch devices.

var isTouchCapable = 'ontouchstart' in window ||
 window.DocumentTouch && document instanceof window.DocumentTouch ||
 navigator.maxTouchPoints > 0 ||
 window.navigator.msMaxTouchPoints > 0;

It's not foolproof since detecting a touch device is apparently an impossibility.

Your mileage may vary:

  • Older touchscreen devices only emulate mouse events
  • Some browsers & OS setups may enable touch APIs when no touchscreen is connected, for example, in systems where a driver for a touchscreen is installed, but the touch hardware is not functioning or not installed.
  • In a hybrid mouse/touch/trackpad/pen/keyboard environment, this doesn't indicate which input is being used, only that the browser detects touch hardware present. For example, a user could switch from using a mouse to touching the screen on a touch-enabled laptop or mouse-connected tablet at any moment. It only detects if the browser believes it could accept or emulate a touch input, for example, when using mobile emulation mode in Chrome Dev Tools.

Update: A tip: Do not use touch-capability detection to control & specify UI behaviors, use event listeners instead. Design for the click/touch/keyup event, not the device, touch-capability detection is typically used to save the cpu/memory expense of adding an event listener. One example of where touch detection could be useful and appropriate:

if (isTouchCapable) {
    document.addEventListener('touchstart', myTouchFunction, false); 
}
like image 28
Benson Avatar answered Sep 23 '22 14:09

Benson