It’s time to stop fooling ourselvs with touch device detection

To all you JS guys out there: It’s time to stop fooling ourselves. The fact that a user has a touch capable device doesn’t mean he’ll be using it with touch only.

Not sure what I’m talking about? There you go:

 var clickEventToUse = ( 'ontouchstart' in window ? 'touchend' : 'click' )

Still not sure?

Now imagine a laptop with touchscreen (it’s a rising number of laptops). This code forces the user in using only the touch capabilites of the laptop even though he might be just willing to do the things with a keyboard and a mouse.

Now imagine the annoyance if the user is working with additional display that is without touch screen. Yep – you have to move the window to the other screen in order to do the thing you want to.

And the fix can be actually simple:

function doSomething() {
    // don't look, do something
$( 'selector' ).on( 'click', function( e ) {
}).on( 'touchend', function( e ) {
    e.preventDefault(); // we don't want the click triggered second time, do we?

P.S.: I do have such laptop and it’s annoying!


Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.