Skip to content

Instantly share code, notes, and snippets.

@yomotsu
Forked from Takazudo/ua.js
Created January 18, 2013 09:40
Show Gist options
  • Save yomotsu/4563459 to your computer and use it in GitHub Desktop.
Save yomotsu/4563459 to your computer and use it in GitHub Desktop.
/* UA detection */
var ua = ( function () {
var ua = {};
var navigator = window.navigator;
var platforms = [
{
identity : 'ie9',
regex : [
{ property : 'userAgent', characters : /MSIE 9\./ }
]
},
{
identity : 'ie10',
regex : [
{ property: 'userAgent', characters: /MSIE 10\./ }
]
},
{
identity : 'firefox',
regex : [
{ property: 'userAgent', characters: /Firefox/ }
]
},
{
identity : 'safari',
regex : [
{ property: 'userAgent', characters: /Safari/ },
{ property: 'vendor', characters: /Apple/ }
]
},
{
identity : 'chrome',
regex : [
{ property: 'userAgent', characters: /Chrome/ },
{ property: 'vendor', characters: /Google/ }
]
},
{
identity : 'opera',
regex : [
{ property: 'userAgent', characters: /Opera/ }
]
},
{
identity : 'iPhone',
regex : [
{ property: 'userAgent', characters: /iPhone/ }
]
},
{
identity : 'iPod',
regex : [
{ property: 'userAgent', characters: /iPod/ }
]
},
{
identity : 'iPad',
regex : [
{ property: 'userAgent', characters: /iPad/ }
]
},
{
identity : 'blackberry',
regex : [
{ property: 'userAgent', characters: /Blackberry/ }
]
},
{
identity : 'android',
regex : [
{ property: 'userAgent', characters: /Android/ }
]
},
{
identity : 'windowsPhone',
regex : [
{ property: 'userAgent', characters: /Windows Phone/ }
]
},
{
identity : 'mac',
regex : [
{ property: 'platform', characters: /Mac/ }
]
},
{
identity : 'windows',
regex : [
{ property: 'platform', characters: /Win/ },
{ property: 'userAgent', characters: /Windows Phone/, match : false }
]
},
{
identity : 'linux',
regex : [
{ property: 'platform', characters: /Linux/ }
]
}
];
var detect = function ( regexes ) {
for ( var i = 0, l = regexes.length, reg; i < l; i ++ ){
reg = regexes[ i ];
if ( !reg.characters.test( navigator[ reg.property ] ) ) {
return false;
}
if( reg.match === false && reg.characters.test( navigator[ reg.property ] ) ){
return false;
}
}
return true;
}
for ( var i = 0, l = platforms.length, platform; i < l; i ++ ){
platform = platforms[ i ];
ua[ platform.identity ] = detect( platform.regex );
}
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('chrome: ' + ua.chrome);
log('safari: ' + ua.safari);
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();
console.log( ua );
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment