-
-
Save yomotsu/4563459 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* 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