Skip to content

Instantly share code, notes, and snippets.

@earlonrails
Last active December 15, 2015 13:19
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 1 You must be signed in to fork a gist
  • Save earlonrails/5266945 to your computer and use it in GitHub Desktop.
Save earlonrails/5266945 to your computer and use it in GitHub Desktop.
Get Browser type and version, cleaned up from http://stackoverflow.com/a/5918791/1354978. Now also checks if it is a mobile device. Now works with IE11!
var getBrowser = function(){
var navigatorObj = navigator.appName,
userAgentObj = navigator.userAgent,
matchVersion;
var match = userAgentObj.match(/(opera|chrome|safari|firefox|msie|trident)\/?\s*(\.?\d+(\.\d+)*)/i);
if( match && (matchVersion = userAgentObj.match(/version\/([\.\d]+)/i)) !== null) match[2] = matchVersion[1];
//mobile
if (navigator.userAgent.match(/iPhone|Android|webOS|iPad/i)) {
return match ? [match[1], match[2], mobile] : [navigatorObj, navigator.appVersion, mobile];
}
// web browser
return match ? [match[1], match[2]] : [navigatorObj, navigator.appVersion, '-?'];
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment