Skip to content

Instantly share code, notes, and snippets.

@kyleweiner
Created March 2, 2013 07:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kyleweiner/5070007 to your computer and use it in GitHub Desktop.
Save kyleweiner/5070007 to your computer and use it in GitHub Desktop.
jQuery: A jQuery function to detect mobile devices.
/**
* @function isMobile
* @description a jQuery function to detect mobile devices
* @param userAgents [array]
* @return object
*/
var userAgents = ['iPad', 'iPhone', 'Android', 'IEMobile', 'BlackBerry'];
function isMobile(userAgents) {
var userAgent,
isMobile = {
any: false
};
$.each(userAgents, function (index) {
userAgent = userAgents[index];
isMobile[userAgent] = navigator.userAgent.toLowercase().indexOf(userAgent.toLowercase()) > -1;
if (isMobile[userAgent]) isMobile.any = true;
});
return isMobile;
}
@pvolyntsev
Copy link

Typo in toLowercase()
Should be toLowerCase()
Also I'd move var userAgents = inside function isMobile() { }
Also it does not detect Android Tablet

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment