Skip to content

Instantly share code, notes, and snippets.

@girvan
Created May 16, 2012 03:53
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 girvan/2707228 to your computer and use it in GitHub Desktop.
Save girvan/2707228 to your computer and use it in GitHub Desktop.
detect mobile / tablet device and insert into body class
// this project is move to https://github.com/girvan/mobile-detection
(function(){
var device = '', _ipad = 'ipad', _iphone = 'iphone', ua = navigator.userAgent.toLowerCase();
if( ua.indexOf(_ipad) != -1 )
device = 'ios tablet ' + _ipad;
else if( ua.indexOf(_iphone) != -1 )
device = 'ios mobile ' + _iphone;
else if( ua.match(/android/) )
{
device = 'android';
if(ua.indexOf('mobile')==-1)
device += ' tablet';
else
device += ' mobile';
}
if(device)
{
device = 'touch ' + device;
document.documentElement.className += ' ' + device;
}
window._device = device;
}
)();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment