Created
April 23, 2015 11:25
-
-
Save quocnguyen/e5e04d140b9baa4615a8 to your computer and use it in GitHub Desktop.
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 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