Add supportsTouchEvents

This commit is contained in:
Angelos Chalaris
2020-05-04 12:57:23 +03:00
parent 081e65eed2
commit 3b8e815340

View File

@ -0,0 +1,18 @@
---
title: supportsTouchEvents
tags: browser,intermediate
---
Returns `true` if touch events are supported, `false` otherwise.
Check if `ontouchstart` exists in `window` or `window.DocumentTouch` is true and the current `document` is an instance of it.
```js
const supportsTouchEvents = () =>
window &&
('ontouchstart' in window || window.DocumentTouch && document instanceof window.DocumentTouch);
```
```js
supportsTouchEvents(); // true
```