public
Last active

  • Download Gist
ua.js
JavaScript
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44
/* 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();

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.