Skip to content

Instantly share code, notes, and snippets.

@pgib
Created June 19, 2012 19:16
Show Gist options
  • Select an option

  • Save pgib/2955988 to your computer and use it in GitHub Desktop.

Select an option

Save pgib/2955988 to your computer and use it in GitHub Desktop.
set and get cookies
setCookie = (name, value, path, expires, domain, secure) ->
if typeof expires is "undefined"
expireDate = new Date()
expireDate.setTime expireDate.getTime() + (3600000 * 24 * 365)
expires = expireDate.toGMTString()
curCookie = name + "=" + escape(value) + (if (expires) then "; expires=" + expires else "") + (if (path) then "; path=" + path else "") + (if (domain) then "; domain=" + domain else "") + (if (secure) then "; secure" else "")
document.cookie = curCookie
getCookie = (name) ->
nameEQ = name + "="
ca = document.cookie.split(";")
i = 0
while i < ca.length
c = ca[i]
c = c.substring(1, c.length) while c.charAt(0) is " "
return c.substring(nameEQ.length, c.length) if c.indexOf(nameEQ) is 0
i++
null
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment