Huge News!Announcing our $40M Series B led by Abstract Ventures.Learn More
Socket
Sign inDemoInstall
Socket

detect-touch

Package Overview
Dependencies
Maintainers
1
Versions
4
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

detect-touch - npm Package Compare versions

Comparing version 1.0.1 to 1.1.0

16

index.js
'use strict';
function detectTouch() {
if (
'ontouchstart' in window ||
window.navigator.maxTouchPoints > 0 ||
window.navigator.msMaxTouchPoints > 0 ||
window.DocumentTouch && document instanceof DocumentTouch
) {
return true;
} else {
return false;
if (typeof window !== 'undefined') {
return Boolean(
'ontouchstart' in window ||
window.navigator.maxTouchPoints > 0 ||
window.navigator.msMaxTouchPoints > 0 ||
window.DocumentTouch && document instanceof DocumentTouch
);
}

@@ -14,0 +12,0 @@ }

{
"name": "detect-touch",
"version": "1.0.1",
"version": "1.1.0",
"description": "Detects if a device has a touch interface",

@@ -5,0 +5,0 @@ "main": "index.js",

# Detect Touch Devices
 
[Live touch detection test][liveTest]

@@ -8,2 +8,4 @@

Better than Modernizr's touch device detection, which only detects support for the W3C Touch Events API, and leaves detection of Microsoft's Pointer Events API to a different category (and even then it fails to detect if it's running on a touch device), which can be frustrating when all you really want to know is if a device has a touch interface or not. For details on how `detect-touch` works see the [detection tests](#detection-tests) section below.
WARNING: There is no absolute way to detect a touch interface, and `detect-touch` will not correctly detect a touch interface 100% of the time, but very close to it, try the [live touch detection test][liveTest] with different devices to test out its reliability. Also, just because a device has a touch interface doesn't mean that it doesn't have a mouse as well.

@@ -16,7 +18,7 @@

```terminal
$ npm install detect-touch
$ npm install --save detect-touch
```
 
## Importing `detect-touch`
Import only the `hasTouch` boolean:
Importing only the `hasTouch` boolean:
```javascript

@@ -28,3 +30,3 @@ import { hasTouch } from 'detect-touch';

Import only the `detectTouch` function:
Importing only the `detectTouch` function:
```javascript

@@ -36,3 +38,3 @@ import { detectTouch } from 'detect-touch';

Import both the `hasTouch` boolean and the `detectTouch` function:
Importing both the `hasTouch` boolean and the `detectTouch` function:
```javascript

@@ -48,3 +50,3 @@ import { hasTouch, detectTouch } from 'detect-touch';

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 as either a device has touch capabilities or it does not.
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.
```javascript

@@ -57,3 +59,3 @@ // Using the hasTouch boolean:

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.
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.
```javascript

@@ -65,1 +67,23 @@ // Using the detectTouch function:

```
 
## Detection Tests
`detect-touch` checks to see if the browser implements any of the following:
The standard [W3C Touch Events API][w3cTE] (this is the vast majority of touch devices), by checking for:
```javascript
'ontouchstart' in window
```
[Microsoft's Pointer Events API running on a touch device][maxTP], by checking for:
```javascript
window.navigator.maxTouchPoints > 0 ||
window.navigator.msMaxTouchPoints > 0 // pre IE 11
```
[Firefox's legacy `DocumentTouch`][docT] (which is now obsolete), by checking for:
```javascript
window.DocumentTouch && document instanceof DocumentTouch
```
[w3cTE]: https://www.w3.org/TR/touch-events/
[maxTP]: https://msdn.microsoft.com/en-us/library/dn433244(v=vs.85).aspx#feature_detection_and_touch_support_testing
[docT]: https://developer.mozilla.org/en-US/docs/Web/API/DocumentTouch
SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc