Detecting Windows Tablet (touch windows devices) with Jquery

心已入冬 提交于 2019-12-11 03:45:00

问题


My site run a script which is only useful with the mouse, and which have to be disable on touch screen because it don't work with them.

So to solve it i use this :

var deviceAgent = navigator.userAgent.toLowerCase();

var isTouchDevice = Modernizr.touch || 
(deviceAgent.match(/(iphone|ipod|ipad)/) ||
deviceAgent.match(/(android)/)  || 
deviceAgent.match(/(iemobile)/) || 
deviceAgent.match(/iphone/i) || 
deviceAgent.match(/ipad/i) || 
deviceAgent.match(/ipod/i) || 
deviceAgent.match(/blackberry/i) || 
deviceAgent.match(/bada/i));

if (!isTouchDevice) {
//my fonction
        }

But this code doesn't detect other touch devices mainly like windows tablets (i have one), and i would like to detect with deviceAgent or anything else the other touch devices, or all touch devices if it's possible.

Thank you very much for your help !


回答1:


This should do it.. I have tested on ios safari, osx safari, windows IE, windows Chrome, windows Firefox, android.

var isTouchDevice    = 'ontouchstart' in window || (navigator.msMaxTouchPoints>0);

Basically, we let the browser determine if there is a touch device and then look for the browser specific implementations of touch events.

Internet explorer 9/10/11

navigator.msMaxTouchPoints

All others:

'ontouchstart' in window


来源:https://stackoverflow.com/questions/28860930/detecting-windows-tablet-touch-windows-devices-with-jquery

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!