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

In an attempt to follow best practices, we try to use the appropriate JavaScript / jQuery events, depending on which device you are using. For example, we create a mobile site with a tag that will have an onclick or touch event. In the case of the iPhone, we would like to use the "touchstart" event. We want to check whether their device supports touchstart before associating this handler with an object. If it is not, then we will link onclick.

What is the best way to do this?

+43
javascript jquery unobtrusive-javascript iphone mobile
May 26 '10 at 18:54
source share
4 answers

You can determine if the event is supported:

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

Take a look at this article:

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

+80
May 26 '10 at 19:03
source share

Use this code to determine if a device supports touch connectivity.

We also work for Windows 8 IE10, which uses the event "MSPointerDown" instead of "touchmove"

 var supportsTouch = false; if ('ontouchstart' in window) { //iOS & android supportsTouch = true; } else if(window.navigator.msPointerEnabled) { //Win8 supportsTouch = true; } 
+12
Jan 14 '13 at 14:44
source share

You can check if typeof document.body.ontouchstart == "undefined" returns to normal dom events

+2
May 26 '10 at 19:05
source share

I made a full demo that works in every browser with the full source code for solving this problem: Detecting touch screen devices in Javascript .

+2
May 12 '11 at 3:09
source share



All Articles