Skip to content

Instantly share code, notes, and snippets.

@quocnguyen
Created April 23, 2015 11:25
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 quocnguyen/e5e04d140b9baa4615a8 to your computer and use it in GitHub Desktop.
Save quocnguyen/e5e04d140b9baa4615a8 to your computer and use it in GitHub Desktop.
var detectDevice = function(userAgent, screenWidth) {
var ua = userAgent.toLowerCase();
var isMobile = {
Android: function() {
return ua.match(/Android/i);
},
BlackBerry: function() {
return ua.match(/BlackBerry/i);
},
iOS: function() {
return ua.match(/iPhone|iPad|iPod/i);
},
Opera: function() {
return ua.match(/Opera Mini/i);
},
Windows: function() {
return ua.match(/IEMobile/i);
},
any: function() {
return (isMobile.Android() || isMobile.BlackBerry() || isMobile.iOS() || isMobile.Opera() || isMobile.Windows());
}
};
if (isMobile.any()) {
if (screenWidth <= 480) {
return 'mobile';
} else {
return 'tablet';
}
} else {
return 'desktop';
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment