Created
June 6, 2019 16:23
-
-
Save atsea/8cd065756478f1a2c3aa9a1e0f224431 to your computer and use it in GitHub Desktop.
JS - Detect touch screen
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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