Skip to content

Instantly share code, notes, and snippets.

@juanbrujo
Created July 7, 2013 22:32
Show Gist options
  • Save juanbrujo/5945220 to your computer and use it in GitHub Desktop.
Save juanbrujo/5945220 to your computer and use it in GitHub Desktop.
Gets the current browser + if it is IE, the version currentBrowser().browser to get the browsername currentBrowser().version to get the version if it is IE
function currentBrowser() {
$.returnVal = "";
var browserUserAgent = navigator.userAgent;
if (browserUserAgent.indexOf("Firefox") > -1) {
$.returnVal = { browser: "Firefox" };
}
else if (browserUserAgent.indexOf("Chrome") > -1) {
$.returnVal = { browser: "Chrome" };
}
else if (browserUserAgent.indexOf("Safari") > -1) {
$.returnVal = { browser: "Safari" };
}
else if (browserUserAgent.indexOf("MSIE") > -1) {
var splitUserAgent = browserUserAgent.split(";");
for (var val in splitUserAgent) {
if (splitUserAgent[val].match("MSIE")) {
var IEVersion = parseInt(splitUserAgent[val].substr(5, splitUserAgent[val].length));
}
}
$.returnVal = { browser: "IE", version: IEVersion };
}
else if (browserUserAgent.indexOf("Opera") > -1) {
$.returnVal = { browser: "Opera" };
}
else {
$.returnVal =
{ browser: "other" };
}
return $.returnVal;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment