matt matt - 7 days ago 4
Javascript Question

How to determine if the client is a touch device

is there any nice and clean method or trick to find out if the user is on a touch-device or not?

I know there is stuff like

var isiPad = navigator.userAgent.match(/iPad/i) != null;


but I simply wonder if there is a trick to generally determine if the user is on Touch device?
Because there are a lot more touch devices and tablets out there then just iPads.

thank you.

Answer

http://alastairc.ac/2010/03/detecting-touch-based-browsing/

function isTouchDevice() {
   var el = document.createElement('div');
   el.setAttribute('ongesturestart', 'return;'); // or try "ontouchstart"
   return typeof el.ongesturestart === "function";
}

and here

There may be detection in jquery for mobile and jtouch