Created
April 13, 2012 09:06
-
-
Save tvandervossen/2375269 to your computer and use it in GitHub Desktop.
How I Learned to Stop Worrying and Drop the Semicolon
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
| device = function(){ | |
| var flags = {}, ua = navigator.userAgent, el = document.createElement('div') | |
| function flag(names) { | |
| document.body.className += (document.body.className ? ' ' : '') + names | |
| names = names.split(' ') | |
| for (var i = 0; i < names.length; i++) flags[names[i]] = true | |
| } | |
| if (ua.indexOf('MSIE ') > -1) flag('msie'); | |
| if (ua.indexOf('Firefox') > -1) flag('firefox') | |
| if (ua.indexOf('(iPad') > -1) flag('ios ipad') | |
| if (ua.indexOf('(iPhone') > -1 || ua.indexOf('(iPod') > 1) flag('ios iphone'); | |
| el.setAttribute('ongesturestart', 'return;') | |
| if (typeof el.ongesturestart === 'function') flag('touch') | |
| if (window.navigator.standalone) flag('standalone') | |
| if (window.devicePixelRatio >= 2) flag('retina') | |
| if(!!document.createElement('video').canPlayType) flag('video') | |
| return flags | |
| }() |
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
| device = (function() { | |
| var flags = {}, ua = navigator.userAgent, el = document.createElement('div'); | |
| function flag(names) { | |
| document.body.className += (document.body.className ? ' ' : '') + names; | |
| names = names.split(' '); | |
| for (var i = 0; i < names.length; i++) { | |
| flags[names[i]] = true; | |
| } | |
| } | |
| if (ua.indexOf('MSIE ') > -1) { | |
| flag('msie'); | |
| } | |
| if (ua.indexOf('Firefox') > -1) { | |
| flag('firefox'); | |
| } | |
| if (ua.indexOf('(iPad') > -1) { | |
| flag('ios ipad'); | |
| } | |
| if (ua.indexOf('(iPhone') > -1 || ua.indexOf('(iPod') > 1) { | |
| flag('ios iphone'); | |
| } | |
| el.setAttribute('ongesturestart', 'return;'); | |
| if (typeof el.ongesturestart === 'function') { | |
| flag('touch'); | |
| } | |
| if (window.navigator.standalone) { | |
| flag('standalone'); | |
| } | |
| if (window.devicePixelRatio >= 2) { | |
| flag('retina'); | |
| } | |
| if(!!document.createElement('video').canPlayType) { | |
| flag('video'); | |
| } | |
| return flags; | |
| }()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Line 11,
ua.indexOf('(iPod') > 1, you meant> -1, right?Also, if your intent in using the opening parenthesis there is to prevent catching the string
iPodas part of another word, you could also opt for the\bword boundary character and instead do/\biPod\b/.test(ua)in case the UA ever switches away from parens (and I believe regex is also faster).