Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
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