Reputation: 10905
I am trying to write code that will run either in a browser or as a phonegap application. One of the things I need to do is to decide if I am in an environment that supports touch events or not.
My current problem is that my Chrome (20.0.01132.47 m) returns true
for ('ontouchstart' in window)
but it does not fire the touch events.
When I open the Developer tools settings dialog (the gear icon on the bottom right of the Developer Tools). I get an option to "Emulate touch events". When I check this option the browser fires the touch events. but when it is not checked it does not fire the events but the detection I use still says that the touch events are available.
Can I tell from script if "Emulate touch events" is enabled or not?
JQuery based answers are fine.
Upvotes: 2
Views: 12835
Reputation: 3785
Best way by js:
function is_touch_device() {
return 'ontouchstart' in window || navigator.maxTouchPoints;
}
console.log(is_touch_device())
Upvotes: 0
Reputation: 33378
Can I tell from script if "Emulate touch events" is enabled or not ?
Try to check for more than one finger ;-)
Perhaps this helps?
window.addEventListener('touchstart', function(event) {
var emulate = event.targetTouches.length == 2;
alert(emulate ? true : false);
}, false);
BTW:
'ontouchstart' in window
results in false
(chrome v21.0.1180.60) unless you have closed the developer toolbar.
Upvotes: 1