Skip to content

Instantly share code, notes, and snippets.

@WillSquire
Last active August 29, 2015 14:25
Show Gist options
  • Save WillSquire/940df02960ddb3ca3041 to your computer and use it in GitHub Desktop.
Save WillSquire/940df02960ddb3ca3041 to your computer and use it in GitHub Desktop.
Browser - Returns browser name.
/**
* Gets the browser name or returns an empty string if unknown.
* This function also caches the result to provide for any
* future calls this function might have.
*
* @returns {string}
*/
var browser = function() {
// Return cached result if avalible, else get result then cache it.
if (browser.prototype._cachedResult)
return browser.prototype._cachedResult;
var isOpera = !!window.opera || navigator.userAgent.indexOf(' OPR/') >= 0;
// Opera 8.0+ (UA detection to detect Blink/v8-powered Opera)
var isFirefox = typeof InstallTrigger !== 'undefined';// Firefox 1.0+
var isSafari = Object.prototype.toString.call(window.HTMLElement).indexOf('Constructor') > 0;
// At least Safari 3+: "[object HTMLElementConstructor]"
var isChrome = !!window.chrome && !isOpera;// Chrome 1+
var isIE = /*@cc_on!@*/false || !!document.documentMode; // At least IE6
return (browser.prototype._cachedResult =
isOpera ? 'Opera' :
isFirefox ? 'Firefox' :
isSafari ? 'Safari' :
isChrome ? 'Chrome' :
isIE ? 'IE' :
'');
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment