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

后端 未结 30 2474
花落未央
花落未央 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:30

    This seems to be working fine for me so far:

    //Checks if a touch screen
    is_touch_screen = 'ontouchstart' in document.documentElement;
    
    if (is_touch_screen) {
      // Do something if a touch screen
    }
    else {
      // Not a touch screen (i.e. desktop)
    }
    

提交回复
热议问题