Skip to content

Instantly share code, notes, and snippets.

@EtienneLem
Created April 5, 2012 18:39
Show Gist options
  • Save EtienneLem/2313146 to your computer and use it in GitHub Desktop.
Save EtienneLem/2313146 to your computer and use it in GitHub Desktop.
BrowserDetect (Coffeescript)
# Original BrowserDetect.js: @Teevio (http://teev.io/blog/text/13423292)
# Original idea: @rogie (http://rog.ie/post/9089341529/html5boilerplatejs)
# CoffeeScript implementation: @EtienneLem
class BrowserDetect
@init: ->
browser = @searchString(@dataBrowser) || "An unknown browser"
version = @searchVersion(navigator.userAgent) || @searchVersion(navigator.appVersion) || "An unknown version"
os = @searchString(@dataOS) || "An unknown OS"
html = document.documentElement
html.setAttribute 'data-browser', browser
html.setAttribute 'data-version', version
html.setAttribute 'data-os', os
@searchString: (data) ->
for obj in data
string = obj.string
prop = obj.prop
versionSearchString = obj.versionSearch || obj.identity
if string && string.indexOf(obj.subString) >= 0
@versionSearchString = versionSearchString
return obj.identity
else if prop
@versionSearchString = versionSearchString
return obj.identity
@searchVersion: (dataString) ->
index = dataString.indexOf @versionSearchString
return if index == -1
return parseFloat(dataString.substring([email protected]+1))
@dataBrowser: [
string: navigator.userAgent
subString: "Chrome"
identity: "Chrome"
,
string: navigator.userAgent
subString: "OmniWeb"
versionSearch: "OmniWeb/"
identity: "OmniWeb"
,
string: navigator.vendor
subString: "Apple"
identity: "Safari"
versionSearch: "Version"
,
prop: window.opera
identity: "Opera"
versionSearch: "Version"
,
string: navigator.vendor
subString: "iCab"
identity: "iCab"
,
string: navigator.vendor
subString: "KDE"
identity: "Konqueror"
,
string: navigator.userAgent
subString: "Firefox"
identity: "Firefox"
,
string: navigator.vendor
subString: "Camino"
identity: "Camino"
,
string: navigator.userAgent
subString: "Netscape"
identity: "Netscape"
,
string: navigator.userAgent
subString: "MSIE"
identity: "Explorer"
versionSearch: "MSIE"
,
string: navigator.userAgent
subString: "Gecko"
identity: "Mozilla"
versionSearch: "rv"
,
string: navigator.userAgent
subString: "Mozilla"
identity: "Netscape"
versionSearch: "Mozilla"
]
@dataOS: [
string: navigator.platform
subString: "Win"
identity: "Windows"
,
string: navigator.platform
subString: "Mac"
identity: "Mac"
,
string: navigator.userAgent
subString: "iPhone"
identity: "iPhone/iPod"
,
string: navigator.platform
subString: "Linux"
identity: "Linux"
]
BrowserDetect.init()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment