Skip to content

Instantly share code, notes, and snippets.

@pobing
Last active January 30, 2018 08:01
Show Gist options
  • Save pobing/f76d4de527dbf84b5b1d1843f03b3d0d to your computer and use it in GitHub Desktop.
Save pobing/f76d4de527dbf84b5b1d1843f03b3d0d to your computer and use it in GitHub Desktop.
Some global Javascript method
window.TBS ||= {}
Date::format = (format) ->
o =
'M+': @getMonth() + 1
'd+': @getDate()
'h+': @getHours()
'm+': @getMinutes()
's+': @getSeconds()
'q+': Math.floor((@getMonth() + 3) / 3)
'S': @getMilliseconds()
if /(y+)/.test(format)
format = format.replace(RegExp.$1, (@getFullYear() + '').substr(4 - (RegExp.$1.length)))
for k of o
if new RegExp('(' + k + ')').test(format)
format = format.replace(RegExp.$1, if RegExp.$1.length == 1 then o[k] else ('00' + o[k]).substr(('' + o[k]).length))
format
TBS.makeid = ->
text = ''
possible = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'
i = 0
while i < 8
text += possible.charAt(Math.floor(Math.random() * possible.length))
i++
text
TBS.getParameterByName = (name, url) ->
if !url
url = window.location.href
name = name.replace(/[\[\]]/g, '\\$&')
regex = new RegExp('[?&]' + name + '(=([^&#]*)|&|#|$)')
results = regex.exec(url)
if !results
return null
if !results[2]
return ''
decodeURIComponent results[2].replace(/\+/g, ' ')
TBS.supportHTML5 = ->
test_canvas = document.createElement("canvas")
isSupport = if test_canvas.getContext then true else false
isSupport
TBS.isIE = ->
userAgent = window.navigator.userAgent
userAgent.indexOf("MSIE ") > -1 || userAgent.indexOf("Trident/") > -1 || userAgent.indexOf("Edge/") > -1
TBS.getLocation = (href) ->
l = document.createElement("a")
l.href = href
l
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment