Skip to content

Instantly share code, notes, and snippets.

@ninjascribble
Created March 8, 2013 22:07
Show Gist options
  • Save ninjascribble/5120295 to your computer and use it in GitHub Desktop.
Save ninjascribble/5120295 to your computer and use it in GitHub Desktop.
user agent matching on the client-side
/** 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