Skip to content

Instantly share code, notes, and snippets.

@arturparkhisenko
Last active January 22, 2016 18:09
Show Gist options
  • Select an option

  • Save arturparkhisenko/e98cd8c84abc46698bba to your computer and use it in GitHub Desktop.

Select an option

Save arturparkhisenko/e98cd8c84abc46698bba to your computer and use it in GitHub Desktop.
Browser-and-OS-detect
function BrowserInfo() {
this.supported = false;
this.isBrowser = true;
this.webBrowser = null;
this.webBrowserVer = null;
this.os = 'unknown os';
var detectedUA = null;
if (typeof window === 'undefined' || !window.navigator) {
this.isBrowser = false;
console.info('Not a Browser?');
}
//if (navigator.userAgent.indexOf('Safari')
if (this.isBrowser && navigator.webkitGetUserMedia && window.webkitRTCPeerConnection) {
this.webBrowser = 'chrome';
detectedUA = navigator.userAgent.match(/Chrom(e|ium)\/([0-9]+)\./);
if (detectedUA) {
this.webBrowserVer = parseInt(detectedUA[2], 10) || 0;
}
this.supported = true;
} else if (this.isBrowser && navigator.mozGetUserMedia && window.mozRTCPeerConnection) {
this.webBrowser = 'firefox';
detectedUA = navigator.userAgent.match(/Firefox\/([0-9]+)\./);
if (detectedUA) {
this.webBrowserVer = parseInt(detectedUA[1], 10) || 0;
}
this.supported = true;
} else if (this.isBrowser && navigator.mediaDevices && navigator.userAgent.match(/Edge\/(\d+).(\d+)$/)) {
this.webBrowser = 'edge';
detectedUA = navigator.userAgent.match(/Edge\/(\d+).(\d+)$/);
if (detectedUA) {
this.webBrowserVer = parseInt(detectedUA[2], 10) || 0;
}
this.supported = true;
}
//detect-OS
if (this.isBrowser) {
var osList = {
'win': /Win/,
'mac': /Mac/,
'ios': /(iPhone|iPad|iPod)/,
'linux': /(Linux|X11)/
};
for (var osProp in osList) {
if (osList.hasOwnProperty(osProp)) {
if (osList[osProp].test(navigator.userAgent) !== false) {
this.os = osProp;
break;
}
}
}
}
}
exports.browserInfo = new BrowserInfo();
if (navigator.userAgent.indexOf('Mac') >= 0) {
document.body.className += ' mac';
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment