Best Way to Detect a Touch Screen Device Using JavaScript



The best way to detect a ‘touch screen’ device, is to work around to see whether the touch methods are present within the document model or not.

function checkTouchDevice() {
   return 'ontouchstart' in document.documentElement;
}

Here, you can also use it to detect mobile or desktop, like the following −

if (checkTouchDevice()) {
   // Mobile device
} else {
   // Desktop
}
Updated on: 2020-06-23T13:08:10+05:30

655 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements