egbgdw
Last Updated: February 25, 2016
·
3.681K
· stowball

Detect touch events on all major mobile platforms

Modernizr doesn't currently detect touch events in Windows (Phone) 8.

So, instead you can easily detect touch on iOS, Android, BlackBerry, Bada and Windows (Phone) 8 with the following:

var html = document.documentElement,
    supportsTouch = 'ontouchstart' in window || navigator.msMaxTouchPoints;

if (supportsTouch)
    html.className += ' touch';
else
    html.className += ' no-touch';
}

This will allow you test for touch support in both JavaScript and CSS.

I've got this wrapped up in a little JavaScript plugin, too: https://github.com/izilla/Supports-Touch