Check if the touch events are supported
True when the browser exposes touch events or reports touch points.
function touchSupported() { return 'ontouchstart' in window || navigator.maxTouchPoints > 0; }
Usage
// Desktop Chromium without a touchscreen:
touchSupported(); // → false
matchMedia('(pointer: coarse)').matches; // → false
Know the limits
- Touch support is not touch use: many laptops have both. For layout, the pointer and hover media features describe the primary input, as the last line shows.
- The original also checked DocumentTouch, an obsolete non-standard interface; maxTouchPoints is the standard signal.
The function above is the code the usage lines run. It does not validate its arguments; add checks where it receives untrusted input.
Source & maintenance
Based on Check if the touch events are supported from the original 1loc collection by Nguyen Huu Phuoc. Copyright (c) 2020 phuoc-ng. MIT licensed.
Uses navigator.maxTouchPoints instead of the obsolete DocumentTouch. Executed in Chromium. Original MDX retained as inert source evidence.
Maintained by Oveyon. Source commit: 14b481cf60df. Read the license.
Keep exploring
- Check if a given DOMRect is contained within another DOMRect — True when the target lies inside the container or on its edges.
- Check if two DOMRects intersect — Two rects intersect when they meet on both axes.
- Compare two DOMRects for size and position match — Compare x, y, width and height exactly.