Categories
JavaScript Answers

How to detect touch screen devices with JavaScript?

Spread the love

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.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *