Skip to content

Instantly share code, notes, and snippets.

@amouratoglou
Created April 29, 2019 12:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save amouratoglou/6a49d33c05743066e30c94bb27435375 to your computer and use it in GitHub Desktop.
Save amouratoglou/6a49d33c05743066e30c94bb27435375 to your computer and use it in GitHub Desktop.
Detect if is mobile and do something #javascript
function isMobile() {
if (navigator.userAgent.match(/Mobi/)) {
return true;
}
if ('screen' in window && window.screen.width < 1366) {
return true;
}
var connection = navigator.connection || navigator.mozConnection || navigator.webkitConnection;
if (connection && connection.type === 'cellular') {
return true;
}
return false;
}
(function () {
var mobile = isMobile();
if (mobile) {
console.log('is a mobile device mate!!!')
} else {
console.log('is not mobile!')
}
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment