Skip to content

Instantly share code, notes, and snippets.

@atsea
Created June 6, 2019 16:23
Show Gist options
  • Save atsea/8cd065756478f1a2c3aa9a1e0f224431 to your computer and use it in GitHub Desktop.
Save atsea/8cd065756478f1a2c3aa9a1e0f224431 to your computer and use it in GitHub Desktop.
JS - Detect touch screen
// https://codepen.io/Ferie/pen/vQOMmO
function is_touch_device() {
var prefixes = ' -webkit- -moz- -o- -ms- '.split(' ');
var mq = function(query) {
return window.matchMedia(query).matches;
}
if (('ontouchstart' in window) || window.DocumentTouch && document instanceof DocumentTouch) {
return true;
}
// include the 'heartz' as a way to have a non matching MQ to help terminate the join
// https://git.io/vznFH
var query = ['(', prefixes.join('touch-enabled),('), 'heartz', ')'].join('');
return mq(query);
}
if (is_touch_device()) {
document.write("Your device is Touch");
} else {
document.write("Your device is NOT touch");
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment