Detect It
This has been deprecated in favor ofDetect Touch Devices
detect-touch
will detect if a device has a touch interface. It provides both a boolean one time touch detection on import, as well as a function to run the touch detection at any time.
detect-touch
Installing $ npm install --save detect-touch
detect-touch
Importing Importing only the hasTouch
boolean:
;// orvar hasTouch = hasTouch;
Importing only the detectTouch
function:
;// orvar detectTouch = detectTouch;
Importing both the hasTouch
boolean and the detectTouch
function:
;// orvar hasTouch = hasTouch;var detectTouch = detectTouch;
detect-touch
Using The hasTouch
boolean is established at the time it is imported, and the function to detect a touch device runs only one time. In most cases this is all you need.
// Using the hasTouch boolean:hasTouch === true;// orhasTouch === false;
The detectTouch
function attempts to detect a touch device each time it is called and can be used to check or recheck for a touch device at a specific time. Returns a boolean. For example, if detect-touch
doesn't have access to the window
when it is imported you'll need to wait until it does before checking if a device has a touch interface.
// Using the detectTouch function: === true;// or === false;
Detection Tests
detect-touch
checks to see if the browser implements any of the following:
The standard W3C Touch Events API (this is the vast majority of touch devices), by checking for:
'ontouchstart' in window
Number of touch points (required to detect Microsoft's Pointer Events API running on a touch device, however, devices not implementing Pointer Events may also indicate this), by checking for:
windownavigatormaxTouchPoints > 0 ||windownavigatormsMaxTouchPoints > 0 // pre IE 11
Firefox's legacy DocumentTouch
(which is now obsolete), by checking for:
windowDocumentTouch && document instanceof DocumentTouch