可以通过以下操作确定您是否在移动设备上?使用window.Touch查找当前设备是否为移动设备
if(window.Touch != undefined)
{
//redirect to my mobile site
}
我想做一个小的检查,看看它的移动设备。我不想为此简单检查导入modernizr图书馆。
可以通过以下操作确定您是否在移动设备上?使用window.Touch查找当前设备是否为移动设备
if(window.Touch != undefined)
{
//redirect to my mobile site
}
我想做一个小的检查,看看它的移动设备。我不想为此简单检查导入modernizr图书馆。
好的在广泛搜索后发现了一些东西。 window.touch doesn't work on Android至少这是别人说的话。我无法证实,因为我没有那么运行Android
移动设备这是我做的,到目前为止,它一直很好:
var HAS_TOUCH = ('ontouchstart' in window);
我使用:
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
}
我在Android 2.3和4.4.2以及iOS 7.1上测试了这个。对于IE我使用了什么Microsoft recommended
使用TouchEvent
只适用于触摸设备;这并不意味着移动设备。对于使用在userAgent
正则表达式:
if(navigator.userAgent.match(/Android/i) || navigator.userAgent.match(/iPhone/i)){
//you are on Android or iPhone
}
但也有很多其他的案件来对待,例如,Windows手机和黑莓手机,所以我建议使用detect mobile API。