My current method for detecting touch events is as follows:
if(window.MSPointerEvent){
//Checking for IE10
}else if(window.PointerEvent){
//Identifying IE11
}else if(window.TouchEvent){
//Recognizing Android and Safari
}else{
//Handling devices without touch events
}
I have tested this approach on various devices, including Android 2.3 and 4.4.2, as well as iOS 7.1. To determine compatibility with Internet Explorer, I followed Microsoft's recommendations.
It's important to note that using TouchEvent
alone may not cover all touch-enabled devices, such as mobile devices. In such cases, it's advisable to use a regular expression on the userAgent
:
if(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/iPhone/i)){
//Detecting Android or iPhone devices
}
However, there are many other scenarios to consider, like Windows Phone and BlackBerry. In these instances, I recommend utilizing the detect mobile API.