Last active
January 29, 2019 15:13
-
-
Save willwright82/9363cc9ca3bdd5de9c6d9bdd32c5ffa8 to your computer and use it in GitHub Desktop.
Detects mobile devices — Usage: if ( mobileCheck.ios ) { // Code }
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
var mobileCheck = { | |
ios: (function(){ | |
return navigator.userAgent.match(/iPhone|iPad|iPod/i); | |
}()), | |
android: (function(){ | |
return navigator.userAgent.match(/Android/i); | |
}()), | |
blackBerry: (function(){ | |
return navigator.userAgent.match(/BB10|Tablet|Mobile/i); | |
}()), | |
windows: (function(){ | |
return navigator.userAgent.match(/IEMobile/i); | |
}()), | |
smartphone: (function(){ | |
return (window.innerWidth <= 384 && window.innerHeight <= 640); | |
}()), | |
tablet: (function(){ | |
return (navigator.userAgent.match(/Tablet|iPad|iPod/i) && window.innerWidth <= 1280 && window.innerHeight <= 800); | |
}()), | |
all: (function(){ | |
return navigator.userAgent.match(/Android|BlackBerry|Tablet|Mobile|iPhone|iPad|iPod|Opera Mini|IEMobile/i); | |
}()) | |
}; | |
// OR: | |
var | |
ua = navigator.userAgent, | |
browser = /Edge\/\d+/.test(ua) ? 'ed' : /MSIE 9/.test(ua) ? 'ie9' : /MSIE 10/.test(ua) ? 'ie10' : /MSIE 11/.test(ua) ? 'ie11' : /MSIE\s\d/.test(ua) ? 'ie?' : /rv\:11/.test(ua) ? 'ie11' : /Firefox\W\d/.test(ua) ? 'ff' : /Chrome\W\d/.test(ua) ? 'gc' : /Chromium\W\d/.test(ua) ? 'oc' : /\bSafari\W\d/.test(ua) ? 'sa' : /\bOpera\W\d/.test(ua) ? 'op' : /\bOPR\W\d/i.test(ua) ? 'op' : typeof MSPointerEvent !== 'undefined' ? 'ie?' : '', | |
os = /Windows NT 10/.test(ua) ? "win10" : /Windows NT 6\.0/.test(ua) ? "winvista" : /Windows NT 6\.1/.test(ua) ? "win7" : /Windows NT 6\.\d/.test(ua) ? "win8" : /Windows NT 5\.1/.test(ua) ? "winxp" : /Windows NT [1-5]\./.test(ua) ? "winnt" : /Mac/.test(ua) ? "mac" : /Linux/.test(ua) ? "linux" : /X11/.test(ua) ? "nix" : "", | |
mobile = /IEMobile|Windows Phone|Lumia/i.test(ua) ? 'w' : /iPhone|iP[oa]d/.test(ua) ? 'i' : /Android/.test(ua) ? 'a' : /BlackBerry|PlayBook|BB10/.test(ua) ? 'b' : /Mobile Safari/.test(ua) ? 's' : /webOS|Mobile|Tablet|Opera Mini|\bCrMo\/|Opera Mobi/i.test(ua) ? 1 : 0, | |
tablet = /Tablet|iPad/i.test(ua), | |
touch = 'ontouchstart' in document.documentElement; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment