Skip to content

Instantly share code, notes, and snippets.

@Takazudo
Created May 18, 2011 23:49
Show Gist options
  • Save Takazudo/979855 to your computer and use it in GitHub Desktop.
Save Takazudo/979855 to your computer and use it in GitHub Desktop.
/* mobile UA detection http://jsfiddle.net/Takazudo/rxPYk/ */
var ua = (function(){
var ua = {};
var navigator = window.navigator;
var platforms = [
{ property: 'platform', regex: /iPhone/i, identity: 'iPhone' },
{ property: 'platform', regex: /iPod/i, identity: 'iPod' },
{ property: 'userAgent', regex: /iPad/i, identity: 'iPad' },
{ property: 'userAgent', regex: /Blackberry/i, identity: 'Blackberry' },
{ property: 'userAgent', regex: /Android/i, identity: 'Android' },
{ property: 'platform', regex: /Mac/i, identity: 'Mac' },
{ property: 'platform', regex: /Win/i, identity: 'Windows' },
{ property: 'platform', regex: /Linux/i, identity: 'Linux' }
];
for(var i=0, l=platforms.length, platform; i<l; i++){
platform = platforms[i];
ua[platform.identity] = platform.regex.test(navigator[platform.property]);
}
ua.AndroidTablet = ua.Android && !/mobile/i.test(navigator.userAgent);
ua.tablet = ua.iPad || ua.AndroidTablet;
return ua;
})();
/* test */
function test(){
var $ul = $('#log');
function log(text){
$('<li>' + text + '</li>').appendTo($ul);
}
log('iPhone: ' + ua.iPhone);
log('iPad: ' + ua.iPad);
log('iPod: ' + ua.iPod);
log('Blackberry: ' + ua.Blackberry);
log('Android: ' + ua.Android);
log('Mac: ' + ua.Mac);
log('Windows: ' + ua.Windows);
log('Linux: ' + ua.Linux);
log('AndroidTablet: ' + ua.AndroidTablet);
log('tablet: ' + ua.tablet);
}
test();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment