How to check browser for touchstart support using JS/jQuery?

JavascriptJqueryIphoneMobileUnobtrusive Javascript

Javascript Problem Overview


Javascript Solutions


Solution 1 - Javascript

You can detect if the event is supported by:

if ('ontouchstart' in document.documentElement) {
  //...
}

Give a look to this article:

The isEventSupported function published there, is really good at detecting a wide variety of events, and it's cross-browser.

Solution 2 - Javascript

Use this code to detect if the device supports touch.

Also work for windows 8 IE10 which uses 'MSPointerDown' event instead of 'touchmove'

var supportsTouch = false;
if ('ontouchstart' in window) {
    //iOS & android
    supportsTouch = true;

} else if(window.navigator.msPointerEnabled) {
    
    // Windows
    // To test for touch capable hardware 
    if(navigator.msMaxTouchPoints) {
        supportsTouch = true;
    }

}

Solution 3 - Javascript

You could check if typeof document.body.ontouchstart == "undefined" to fall back to normal dom events

Solution 4 - Javascript

I made a full demostration that works in every browser with the full source code of the solution of this problem: Detect touch screen devices in Javascript.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionAlexView Question on Stackoverflow
Solution 1 - JavascriptChristian C. SalvadóView Answer on Stackoverflow
Solution 2 - JavascriptGeorge FilippakosView Answer on Stackoverflow
Solution 3 - Javascriptantimatter15View Answer on Stackoverflow
Solution 4 - JavascriptManuel Ignacio López QuinteroView Answer on Stackoverflow