Skip to content

Instantly share code, notes, and snippets.

@bfricka
Created January 1, 2013 06:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save bfricka/4425653 to your computer and use it in GitHub Desktop.
Save bfricka/4425653 to your computer and use it in GitHub Desktop.
Simple User Agent
###
Adapted from jqMobi (appMobi)
https://github.com/appMobi/jQ.Mobi
MIT License
###
window.UserAgent = ->
self = @
self.ua = navigator.userAgent
self.os = {}
detectUA = (ua) ->
self.os.webkit = (if ua.match(/WebKit\/([\d.]+)/) then true else false)
self.os.android = (if ua.match(/(Android)\s+([\d.]+)/) or ua.match(/Silk-Accelerated/) then true else false)
self.os.androidICS = (if self.os.android and ua.match(/(Android)\s4/) then true else false)
self.os.ipad = (if ua.match(/(iPad).*OS\s([\d_]+)/) then true else false)
self.os.iphone = (if not self.os.ipad and ua.match(/(iPhone\sOS)\s([\d_]+)/) then true else false)
self.os.webos = (if ua.match(/(webOS|hpwOS)[\s\/]([\d.]+)/) then true else false)
self.os.touchpad = (if self.os.webos and ua.match(/TouchPad/) then true else false)
self.os.ios = self.os.ipad or self.os.iphone
self.os.ios6 = (if self.os.ios and ua.match(/(OS)\s([6])/) then true else false)
self.os.playbook = (if ua.match(/PlayBook/) then true else false)
self.os.blackberry = (if self.os.playbook or ua.match(/BlackBerry/) then true else false)
self.os.blackberry10 = (if self.os.blackberry and ua.match(/Safari\/536/) then true else false)
self.os.chrome = (if ua.match(/Chrome/) then true else false)
self.os.opera = (if ua.match(/Opera Mobi/) then true else false)
self.os.fennec = (if ua.match(/fennec/i) then true else false)
self.os.supportsTouch = Modernizr.touch
self.os.desktop = not (self.os.ios or self.os.android or self.os.blackberry or self.os.opera or self.os.fennec or self.os.supportsTouch)
detectUA(self.ua)
self.os
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment