comments 9

Detect Touch Screen with JavaScript

We can detect touch screen with JavaScript let’s start it with creating a simple function ‘isTouchDevice();’ which will return true if its touch device.

function isTouchDevice(){
    return true == ("ontouchstart" in window || window.DocumentTouch && document instanceof DocumentTouch);
}

Now checking if ‘isTouchDevice();’ is returns true it means its a touch device.

if(isTouchDevice()===true) {
    alert('Touch Device'); //your logic for touch device
}
else {
    alert('Not a Touch Device'); //your logic for non touch device
}

Working Fiddle Example

Devices Supported:

Tested and verified on following devices;
1) IPhone 4s, IPhone 5
2) IPad, IPad mini
3) Samsung galaxy S5, Samsung Note 3
Works great on all mentioned devices and will work on other devices as well. Let me know in comments if you have tested and verified on other devices.

  • disqus_iPG2dAX4oF

    on my desktop (without touchscreen), it func returning true in Google Chrome 39.0.2171.95 m

    • Aamir Shahzad

      I have verified it. Fortunately, I have same chrome version. It’s working fine. May be you are confused with the alert as for desktop it says ‘Not a Touch Device’ for touch device it will says ‘touch device’. Below is the screenshot for verification. Thanks for the comment. Let me know if can help you with anything else.

      • disqus_iPG2dAX4oF

        Thanks,
        I guess it just have some troubles with some windows 8 settings.

        • Aamir Shahzad

          I will investigate this issue on windows 8.
          Will get back to you ASAP.

    • Aamir Shahzad

      I have verified it on windows 8.1 its working fine. Thanks

  • hayesmaker

    what does the function return if it’s NOT a touch screen? false or “undefined”?

    • Aamir Shahzad

      Bro. It will return Boolean. Give a try to it putting following alert in else;
      alert(typeof(isTouchDevice()));

  • Kaushik Thanki

    Hi Aamir ,

    It’s working fine on chrome , However it’s not working in mozila firefox 39.

  • Erich Brunner

    Thanks for sharing that solution Aamir 😁