Oscar Godson Oscar Godson - 6 months ago 25
Javascript Question

How can I check for real touch support on a browser

Today (or very recently) Chrome Beta updated to 17 for me and with it i noticed some funkiness in my web app. I noticed it was because a class was being added to the body element that normally only gets put there if there is touch event support which I check like this:

try {
document.createEvent("TouchEvent");
_device.touch = true;
} catch (e) {
_device.touch = false;
}


And sure enough, i can create and trigger touch events on Chrome 17. First idea i had was, oh, i can check for touch, and see if a mouse click fails, therefore, there's a mouse, but MouseEvents trigger too.

How else can I check, without user agent sniffing, that it's an actual, touchable, device, and not just a browser that supports touch events.

Answer

Try:

if('ontouchstart' in document.documentElement) {
    //Do something for touch
} else {
    //Do something NOT for touch
}