Last active
January 22, 2016 18:09
-
-
Save arturparkhisenko/e98cd8c84abc46698bba to your computer and use it in GitHub Desktop.
Browser-and-OS-detect
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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(); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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