Skip to content

Instantly share code, notes, and snippets.

@ixisio
Forked from tvandervossen/environment.js
Last active December 11, 2015 20:28
Show Gist options
  • Save ixisio/4655330 to your computer and use it in GitHub Desktop.
Save ixisio/4655330 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
}
// Detect User Agents
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')
// Detect Touch Devices
if ('createTouch' in document) flag('touch')
if (window.navigator.msPointerEnabled) flag('mspointer')
if (window.navigator.msPointerEnabled && window.navigator.msMaxTouchPoints) flag('touch')
// Detect Fullscreen / App Mode
if ( ('standalone' in window.navigator) && window.navigator.standalone)
flag('standalone')
// Detect Retina Displays
if (window.devicePixelRatio >= 2) flag('retina')
// Detect iframes
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