Skip to content

Instantly share code, notes, and snippets.

@dpesch
Forked from tvandervossen/environment.js
Last active December 18, 2015 01:49
Show Gist options
  • Save dpesch/5706696 to your computer and use it in GitHub Desktop.
Save dpesch/5706696 to your computer and use it in GitHub Desktop.
device = function(){
var flags = {}, ua = navigator.userAgent, el = document.createElement('div'), root = document.documentElement, i
function flag(names) {
root.className += (root.className ? ' ' : '') + names
names = names.split(' ')
for (i = 0; i < names.length; i++) flags[names[i]] = true
}
if (ua.indexOf('WebKit/') > -1) flag('webkit')
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')
if (!flags.ios) flag('desktop')
el.setAttribute('ontouchstart', 'return;')
if (typeof el.ontouchstart === 'function') flag('touch')
if (window.navigator.msPointerEnabled) flag('touch mspointer')
if (window.navigator.standalone) flag('standalone')
if (window.devicePixelRatio >= 2) flag('retina')
if (window !== window.top) flag('embedded')
return flags
}()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment