Skip to content

Instantly share code, notes, and snippets.

@bloodyowl
Created September 15, 2012 12:00
Show Gist options
  • Save bloodyowl/3727495 to your computer and use it in GitHub Desktop.
Save bloodyowl/3727495 to your computer and use it in GitHub Desktop.
Browser detection
/*
Simple browser detection script.
Returns an object.
*/
var Browser = new function() {
this.UA = window.navigator.userAgent.toLowerCase();
this.isChrome = !!(this.UA.indexOf("chrome") + 1);
this.isSafari = !!(this.UA.indexOf("safari") + 1);
this.isIE = !!(this.UA.indexOf("msie") + 1);
this.isIE6 = !!(this.UA.indexOf("msie 6.0") + 1);
this.isIE7 = !!(this.UA.indexOf("msie 7.0") + 1);
this.isIE8 = !!(this.UA.indexOf("msie 8.0") + 1);
this.isOpera = !!(this.UA.indexOf("opera") + 1);
this.isKonqueror = !!(this.UA.indexOf("konqueror") + 1);
this.isiPhone = !!(this.UA.indexOf("iphone") + 1);
this.isiPad = !!(this.UA.indexOf("ipad") + 1);
this.isiPodTouch = !!(this.UA.indexOf("ipod") + 1);
this.isiOS = !!(this.isiPhone + this.isiPad + this.isiPodTouch);
this.isAndroid = !!(this.UA.indexOf("android") + 1);
// returns a string that can be used to define a className for document.documentElement
this.toClassName = function(){
var className = "";
for(var i in this) if (i.indexOf("is") === 0 && typeof this[i] === "boolean" && this[i]) className += i.slice(2,i.length).toLowerCase() + " ";
return className.slice(0, className.length - 1)
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment