What's the best way to detect a 'touch screen' device using JavaScript?

后端 未结 30 2926
花落未央
花落未央 2020-11-21 23:50

I\'ve written a jQuery plug-in that\'s for use on both desktop and mobile devices. I wondered if there is a way with JavaScript to detect if the device has touch screen capa

30条回答
  •  日久生厌
    2020-11-22 00:19

    Using all the comments above I've assembled the following code that is working for my needs:

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

    I have tested this on iPad, Android (Browser and Chrome), Blackberry Playbook, iPhone 4s, Windows Phone 8, IE 10, IE 8, IE 10 (Windows 8 with Touchscreen), Opera, Chrome and Firefox.

    It currently fails on Windows Phone 7 and I haven't been able to find a solution for that browser yet.

    Hope someone finds this useful.

提交回复
热议问题