Created
March 8, 2013 22:07
-
-
Save ninjascribble/5120295 to your computer and use it in GitHub Desktop.
user agent matching on the client-side
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
/** User-agent pattern matching */ | |
var browser = 'unknown' | |
, version = 'unknown' | |
, patterns = { | |
android: /android\s([0-9.]*)/i | |
chrome: /chrome\/([0-9.]*)/i, | |
firefox: /firefox\/([0-9.]*)/i, | |
ie: /msie\s([0-9.]*)/i, | |
ipad: /ipad.version\/([0-9.]*).safari/i, | |
iphone_4: /iphone\sos\s4.version.mobile\/([0-9.]*).safari/i, | |
iphone_5: /iphone\sos\s5.version.mobile\/([0-9.]*).safari/i, | |
safari: /version\/([0-9.]*).safari/i | |
} | |
, key, matches; | |
for (key in patterns) { | |
matches = navigator.userAgent.match(patterns[key]); | |
if (matches) { | |
browser = key; | |
version = matches[1]; | |
break; | |
} | |
} | |
console.log(browser, version); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment