Monday, May 20, 2024
68
rated 0 times [  73] [ 5]  / answers: 1 / hits: 26494  / 12 Years ago, mon, january 21, 2013, 12:00:00

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...


More From » google-chrome

 Answers
4

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.


[#80730] Saturday, January 19, 2013, 12 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.
felixa

Total Points: 180
Total Questions: 113
Total Answers: 108

Location: Palau
Member since Sat, Aug 21, 2021
3 Years ago
;