I'm using:
if(window.MSPointerEvent){
//you are on IE10
}else if(window.PointerEvent){
//you are on IE11
}else if(window.TouchEvent){
//android and safari
}else{
//don't have touch events
}
I tested this on Android 2.3 and 4.4.2 and on iOS 7.1. For the IE I used what Microsoft recommended
Using TouchEvent
only works if you are on a touch device; that doesn't mean a mobile device. For that use a regular expression on the userAgent
:
if(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/iPhone/i)){
//you are on Android or iPhone
}
But there are a lot of other cases to treat, for example, Windows Phone and BlackBerry, so I recommend using the detect mobile API.