Sometimes, we want to detect touch screen devices with JavaScript.
In this article, we’ll look at how to detect touch screen devices with JavaScript.
How to detect touch screen devices with JavaScript?
To detect touch screen devices with JavaScript, we check if the ontouchstart
property is in document.documentElement
.
For instance, we write
const isTouchDevice = "ontouchstart" in document.documentElement;
to check if the 'ontouchstart'
property is in document.documentElement
to check if the device supports touch.
Conclusion
To detect touch screen devices with JavaScript, we check if the ontouchstart
property is in document.documentElement
.