('ontouchstart' in window) returns true but no touch events

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.

Answers


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

Best way by js:

function is_touch_device() {
    return 'ontouchstart' in window || navigator.maxTouchPoints;
}
console.log(is_touch_device())

Need Your Help

Introspect function to determine which argument unpacking (positional or keyword) was used

python python-3.x introspection argument-passing kwargs

I was searching for a way to identify whether some argument is used for unpacking and I have found this:

Invalid Cast Exception in HttpFileCollection

c# asp.net httppostedfile httpfilecollection

I have an extension method below, but when I run this, the foreach gives me InvalidCastException and it says *