Last active
October 29, 2015 21:50
-
-
Save mhulse/9a1f46399e1d6655b904 to your computer and use it in GitHub Desktop.
Super simple browser version and OS sniffer.
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
| navigator.os = (function(navigator) { | |
| 'use strict'; | |
| var os = ''; | |
| var i; | |
| var l; | |
| var map1 = ['win', 'mac', 'x11', 'linux']; | |
| var map2 = ['Windows', 'Mac', 'Unix', 'Linux']; | |
| for (i = 0, l = map1.length; i < l; i++) { | |
| if (navigator.appVersion.toLowerCase().indexOf(map1[i]) != -1) { | |
| os = map2[i]; | |
| break; | |
| } | |
| } | |
| return os; | |
| })(navigator); |
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
| /*! https://gist.github.com/mhulse/9a1f46399e1d6655b904 */navigator.os=function(n){"use strict";var i,a,r="",o=["win","mac","x11","linux"],e=["Windows","Mac","Unix","Linux"];for(i=0,a=o.length;a>i;i++)if(-1!=n.appVersion.toLowerCase().indexOf(o[i])){r=e[i];break}return r}(navigator); |
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
| <!doctype html> | |
| <html lang=en> | |
| <head> | |
| <meta charset=utf-8> | |
| <title>blah</title> | |
| </head> | |
| <body> | |
| <script src="simple-sniffer.min.js"></script> | |
| <script> | |
| window.onload = function() { | |
| $test = document.getElementById('test'); | |
| $test.innerText = navigator.what.join(', '); | |
| } | |
| </script> | |
| <div id="test"></div> | |
| </body> | |
| </html> |
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
| // http://stackoverflow.com/q/11219582/922323 | |
| navigator.what = (function(navigator) { | |
| 'use strict'; | |
| var version = navigator.appVersion; | |
| var browser = (function() { | |
| var name = navigator.appName; | |
| var ua = navigator.userAgent; | |
| var match = ua.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i); | |
| var temp = ua.match(/version\/([\.\d]+)/i); | |
| if (match && temp) match[2] = temp[1]; | |
| match = match ? [match[1], match[2]] : [name, version]; | |
| return match; | |
| })(); | |
| var os = (function() { | |
| var o = ''; | |
| var i; | |
| var l; | |
| var map1 = ['win', 'mac', 'x11', 'linux']; | |
| var map2 = ['Windows', 'Mac', 'Unix', 'Linux']; | |
| for (i = 0, l = map1.length; i < l; i++) { | |
| if (version.toLowerCase().indexOf(map1[i]) != -1) { | |
| o = map2[i]; | |
| break; | |
| } | |
| } | |
| return o; | |
| })(); | |
| browser.push(os); | |
| return browser; | |
| })(navigator); |
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
| /*! https://gist.github.com/mhulse/9a1f46399e1d6655b904 */navigator.what=function(r){"use strict";var n=r.appVersion,a=function(){var a=r.appName,i=r.userAgent,e=i.match(/(opera|chrome|safari|firefox|msie)\/?\s*(\.?\d+(\.\d+)*)/i),t=i.match(/version\/([\.\d]+)/i);return e&&t&&(e[2]=t[1]),e=e?[e[1],e[2]]:[a,n]}(),i=function(){var r,a,i="",e=["win","mac","x11","linux"],t=["Windows","Mac","Unix","Linux"];for(r=0,a=e.length;a>r;r++)if(-1!=n.toLowerCase().indexOf(e[r])){i=t[r];break}return i}();return a.push(i),a}(navigator); |
Author
Author
Alternative: Use Bowser and the just-os.js script above:
console.info(bowser.name, bowser.version.split('.')[0], navigator.os);
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Unfortunately, I could not get this to detect Edge. No time to figure out. Sorry.