Skip to content

Instantly share code, notes, and snippets.

@jonlabelle
Created June 21, 2010 21:13
Show Gist options
  • Save jonlabelle/447498 to your computer and use it in GitHub Desktop.
Save jonlabelle/447498 to your computer and use it in GitHub Desktop.
(function() {
var jonJS = {
version: "1.1"
};
jonJS.Browser = function() {
var ua = navigator.userAgent.toLowerCase();
// -------------------------
// Browsers
// -------------------------
this.isOpera = ua.indexOf("opera") > -1;
this.isIE = ua.indexOf("msie") > -1;
this.isIEv55_6 = typeof document.addEventListener !== "function" && !window.XMLHttpRequest;
this.isWebKit = ua.indexOf("applewebkit/") > -1;
this.isGecko = ua.indexOf("gecko") > -1 && ua.indexOf("khtml") > -1;
this.isChrome = ua.indexOf("chrome") > -1;
this.isFireFox = ua.indexOf("firefox") > -1;
this.isSafari = ua.indexOf("safari") > -1;
this.isMobileSafari = /apple.*mobile/.test(ua);
// -------------------------
// Browser Version
// -------------------------
this.browserName = navigator.appName;
this.browserVersion = "" + parseFloat(navigator.appVersion);
this.browserMajorVersion = parseInt(navigator.appVersion, 10);
var nameOffset, verOffset, ix;
if (this.isIE) { // get IE version
this.browserName = "MSIE";
this.browserVersion = ua.substring(ua.indexOf("msie") + 5);
}
else if (this.isOpera) { // get Opera version
this.browserName = "Opera";
this.browserVersion = ua.substring(ua.indexOf("opera") + 6);
}
else if (this.isChrome) { // get Chrome version
this.browserName = "Chrome";
this.browserVersion = ua.substring(ua.indexOf("chrome") + 7);
}
else if (this.isSafari) { // get Safari version
this.browserName = "Safari";
this.browserVersion = ua.substring(ua.indexOf("safari") + 7);
}
else if (this.isFireFox) { // get FireFox version
this.browserName = "Firefox";
this.browserVersion = ua.substring(ua.indexOf("firefox") + 8);
}
else if ((nameOffset = ua.lastIndexOf(' ') + 1) < (verOffset = nAgt.lastIndexOf('/'))) { // all other browsers
this.browserName = ua.substring(nameOffset, verOffset);
this.browserVersion = ua.substring(verOffset + 1);
if (this.browserName.toLowerCase() == this.browserName.toUpperCase()) {
this.browserName = navigator.appName;
}
}
if ((ix = this.browserVersion.indexOf(";")) != -1) {
this.browserVersion = this.browserVersion.substring(0, ix);
}
if ((ix = this.browserVersion.indexOf(" ")) != -1) {
this.browserVersion = this.browserVersion.substring(0, ix);
}
this.browserMajorVersion = parseInt('' + this.browserVersion, 10);
if (isNaN(this.browserMajorVersion)) {
this.browserVersion = '' + parseFloat(navigator.appVersion);
this.browserMajorVersion = parseInt(navigator.appVersion, 10);
}
// -------------------------
// OS
// -------------------------
var osPlatform = ua.match(/windows/) ? "WindowsOS" : (ua.match(/linux/) ? "LinuxOS" : (ua.match(/mac/) ? "MacOS" : ua.match(/unix/) ? "UnixOS" : "unknown"));
this[osPlatform] = true;
// -------------------------
// .NET Framework
// -------------------------
this.dotNet2 = this.dotNet3 = this.dotNet35 = this.dotNet4 = this.isIE64bit = false;
if (this.WindowsOS) {
this.dotNet2 = ua.indexOf(".net clr 2.0") != -1;
this.dotNet3 = ua.indexOf(".net clr 3.0") != -1;
this.dotNet35 = ua.indexOf(".net clr 3.5") != -1;
this.dotNet4 = ua.indexOf(".net4.0") != -1;
// IE 64-bit
if (ua.indexOf("win64") > -1 && ua.indexOf("x64") > -1) {
this.isIE64bit = true;
}
}
// -------------------------
// Apple mobile devices
// -------------------------
this.isIphone = this.isIpod = this.isIpad = false;
if (this.isMobileSafari) {
this.isIphone = ua.indexOf("iphone") != -1;
this.isIpod = ua.indexOf("ipod") != -1;
this.isIpad = ua.indexOf("ipad") != -1;
}
// -------------------------
// SSL check
// -------------------------
this.isSecure = window.location.href.toLowerCase().indexOf("https") === 0;
};
jonJS.Browser = new jonJS.Browser(); // ini singleton instance
window.jonJS = jonJS; // expose to global obj
})();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment