How to detect touch device browsers vs desktop using javascript?

Alex picture Alex · Sep 9, 2012 · Viewed 13.7k times · Source

What is the code to detect touch devices (smartphones and tablets) vs desktops browsers using userAgent.match and return a boolean variable (for example 'isipad')?

I need to test this against Android and Apple devices mainly. If the device browser is Android or Apple, return isipad = false. Else, return isipad = true.

So far I went about it like this (for iDevice browser detection) :

if( navigator.userAgent.match(/iPhone/i) || navigator.userAgent.match(/iPod/i) ||navigator.userAgent.match(/iPad/i) )
{var isipad = true;}
else
{var isipad = false;}

It seems to work, however I would love to be able to add the android browser as well, in this case.

Thanks in advance. Alex

Answer

Pratswinz picture Pratswinz · Apr 17, 2013
if ("ontouchstart" in document.documentElement)
{
  alert("It's a touch screen device.");
}
else
{
 alert("Others devices");
}

most simple code i found after browsing a lot here and there