How can I detect device touch support in JavaScript?

后端 未结 4 627
孤城傲影
孤城傲影 2020-12-28 18:42

In the past, when detecting whether a device supports touch events in JavaScript, we could do something like this:

var touch_capable = (\'ontouchstart\' in d         


        
4条回答
  •  爱一瞬间的悲伤
    2020-12-28 19:39

    The correct answer is to handle both event types - they're not mutually exclusive.

    For a more reliable test for touch support, also look for window.DocumentTouch && document instanceof DocumentTouch which is one of the tests used by Modernizr

    Better yet, just use Modernizr yourself and have it do the feature detection for you.

    Note though that you cannot prevent false positives, hence my first line above - you've got to support both.

提交回复
热议问题