我正在使用此功能来检测设备是否为触摸设备:
function is_touch_device()
{
return !!('ontouchstart' in window) || !!('onmsgesturechange' in window);
};
从这里获得此功能:What's the best way to detect a 'touch screen' device using JavaScript?
但是自Chrome 25(25.0.1364)起,它在不是触摸屏的桌面上返回true。
另外,我已经将IE9更新为IE10,它在IE中返回true!
进行了搜索,但找不到任何有用的解决方法,只能使用类似http://detectmobilebrowsers.com/的方法:
您有什么推荐的吗?
期待您的回复!
最佳答案
该代码可以正常工作,浏览器能够理解触摸事件,只是因为您的屏幕不可触摸,并不意味着该浏览器不支持该功能。您要测试的是一项硬件功能,它实际上并不是可测试的。您可以始终使用不同的方式,尽管在触摸一次(例如this文章描述)或较大库使用的许多其他工具(例如Modernizer)之后查看用户是否真正使用了触摸界面。
作为引用,以上文章中实际使用的代码是:
function isTouchDevice() {
var el = document.createElement('div');
el.setAttribute('ongesturestart', 'return;');
if(typeof el.ongesturestart == "function"){
return true;
}else {
return false
}
}