-
-
Save weaming/a902be05afc5fb071355aa5b360ef42a 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
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(){ | |
function log(text){ | |
console.log(text) | |
} | |
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