Last active
October 8, 2019 15:48
-
-
Save evild70/9674c1d0d069654aa540c1fecb74d315 to your computer and use it in GitHub Desktop.
isTouchDevice - Determine if device has touch capabilities.
This file contains hidden or 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
var isTouchDevice = false; | |
if (('ontouchstart' in window) || (navigator.maxTouchPoints > 0) || (navigator.msMaxTouchPoints > 0)) { | |
isTouchDevice = true; | |
} | |
var deviceAgent = navigator.userAgent.toLowerCase(); | |
isTouchDevice = (deviceAgent.match(/(iphone|ipod|ipad)/) || deviceAgent.match(/(android)/) || deviceAgent.match(/(iemobile)/) || deviceAgent.match(/iphone/i) || deviceAgent.match(/ipad/i) || deviceAgent.match(/ipod/i) || deviceAgent.match(/blackberry/i) || deviceAgent.match(/bada/i)); | |
// Saw in Modernizr issues thread about the old Modernizr.touch check | |
if {( | |
(window.location.host == 'apps.facebook.com') || // in the facebook app it's difficult to manage media queries (cause of the sidebar) so in desktop devices with touch screen it's tricky | |
(window.screen.width > 1279 && window.devicePixelRatio == 1) || // this number could be probably 1281, there are no so many mobile devices with more than 1280 and pixelRatio equal to 1 (i.e. retina displays are equal to 2...) | |
(window.screen.width > 1000 && window.innerWidth < (window.screen.width * .9)) || // this checks in the end if a user is using a resized browser window which is not common on mobile devices | |
(window.screen.width > 1000 && !this.browser.mobile) // this.browser.mobile is a custom check that can use for instance a common jquery plugin for browser detection | |
) { | |
// then add a class to html | |
$('html').addClass('force-no-touch'); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment