('ontouchstart' in window) returns true but no touch events
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.
Fiddle
Comments
-
epeleg over 1 year
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.
-
Prinzhorn about 11 yearsHere's a list of tests and false positives modernizr.github.com/Modernizr/touch.html
-
-
epeleg about 11 yearsI am not sure how this is supposed to help. If
touchstart
is not fired this code is never called anyway. maybe my Q wasn't clear enough - I am not trying to tell apart touch events that are emulated from "real" ones. I want to know if touchEvents are supported at all. -
Vimil Saju over 10 yearswhy not just create a global flag which is initially set to false. in the ontouch event handler set the flag to true. So if the event fires, the flag gets set to true and you know the device has touch support
-
yckart over 10 years@VimilSaju however you cannot detect if the event-handler-function was called during an emulated event, in this case chromes Emulate touch events-feature. So the global flag will changed on all devices, even on chrome for desktops.
-
Vimil Saju over 10 years@yckart but if emulate touch events is turned on, it is deliberately turned on by the user for a specific reason. Isn't this same as a browser emulating a different user-agent (or an application trying to determine if its running on a VM or a real machine - very few applications would want to know that)? Why would any site want to know if the touch events are emulated or not? if the browser says touch events are supported then that is sufficient, How the support is provided (either through emulation or actual hardware support) should be irrelevant to the site.
-
yckart over 10 yearsYeah, you are completely right. However, this would not answer his question ;)
-
Ming-Tang over 9 yearsI tried that a few times and Google Chrome, with all other tabs, crashed