Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to determine if the client is a touch device [duplicate]

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.

like image 290
matt Avatar asked Jun 07 '11 08:06

matt


People also ask

What is used to check if the app has touch capabilities?

matchMedia Test. Also, we can use the window. matchMedia method to test whether a device has any touchscreen features present.


2 Answers

You can use the following JS function:

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

Source: Detecting touch-based browsing.

Please note the above code only tests if the browser has support for touch, not the device.

Related links:

  • Optimize website for touch devices
  • What is the best way to detect a mobile device in jQuery?
  • What's the best way to detect a 'touch screen' device using JavaScript?
  • Mozilla.org Detecting touch: it’s the ‘why’, not the ‘how’

There may be detection in jquery for mobile and jtouch

like image 127
mplungjan Avatar answered Sep 23 '22 22:09

mplungjan


Include modernizer, which is a tiny feature detection library. Then you can use something like below.

if (Modernizr.touch){    // bind to touchstart, touchmove, etc and watch `event.streamId` } else {    // bind to normal click, mousemove, etc } 
like image 21
Gcoop Avatar answered Sep 21 '22 22:09

Gcoop