Skip to content

Instantly share code, notes, and snippets.

@ralphcrisostomo
Last active December 16, 2015 16:40
Show Gist options
  • Save ralphcrisostomo/5464850 to your computer and use it in GitHub Desktop.
Save ralphcrisostomo/5464850 to your computer and use it in GitHub Desktop.
# TO USE:
# window.app.helpers.browserDetect.initialize()
# window.app.helpers.browserDetect.browser == "Safari"
# window.app.helpers.browserDetect.version == 2
"use strict";
window.app = window.app or {}
window.app.helpers = window.app.helpers or {}
window.app.helpers.browserDetect =
initialize : ->
console.log "Browser Detect Initialize"
@browser = @searchString(@dataBrowser) or "Other"
@version = @searchVersion(navigator.userAgent) or @searchVersion(navigator.appVersion) or "Unknown"
searchString : (data) ->
lock = false
for item in data
dataString = item.string
if dataString.indexOf(item.subString) != -1 and lock is false
@versionSearchString = item.subString
identity = item.identity
lock = true
identity
searchVersion : (dataString) ->
index = dataString.indexOf @versionSearchString
if index is -1
return
parseFloat(dataString.substring(index + @versionSearchString.length + 1))
dataBrowser : [
"string" : navigator.userAgent
"subString" : "Chrome"
"identity" : "Chrome"
,
"string" : navigator.userAgent
"subString" : "MSIE"
"identity" : "Explorer"
,
"string" : navigator.userAgent
"subString" : "Firefox"
"identity" : "Firefox"
,
"string" : navigator.userAgent
"subString" : "Safari"
"identity" : "Safari"
,
"string" : navigator.userAgent
"subString" : "Opera"
"identity" : "Opera"
]
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment