Instantly share code, notes, and snippets.
Last active
November 18, 2022 06:29
-
Star
(0)
0
You must be signed in to star a gist -
Fork
(0)
0
You must be signed in to fork a gist
-
Save jgphilpott/b9ce64b9ef8b04c5ac58902b133b1a28 to your computer and use it in GitHub Desktop.
A collection of functions for writing, reading, updating and deleting cookies.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
# Credit: https://www.w3schools.com/js/js_cookies.asp | |
writeCookie = (key, value, expDays = 365) -> | |
if String key | |
date = new Date() | |
expTime = 1000 * 60 * 60 * 24 * expDays | |
date.setTime date.getTime() + expTime | |
expDate = date.toUTCString() | |
document.cookie = key + "=" + value + "; expires=" + expDate + "; path=/" | |
return true | |
else | |
return false | |
readCookie = (key) -> | |
cookies = readCookies() | |
if key of cookies | |
return cookies[key] | |
else | |
return null | |
readCookies = -> | |
cookies = {} | |
for cookie in document.cookie.split ";" | |
if cookie | |
cookie = decodeURIComponent(cookie).split "=" | |
cookies[cookie[0].trim()] = cookie[1].trim() | |
return cookies | |
updateCookie = (key, value, expDays = 365) -> | |
cookie = readCookie key | |
if cookie != null | |
return writeCookie key, value, expDays | |
else | |
return false | |
deleteCookie = (key) -> | |
cookie = readCookie key | |
if cookie != null | |
date = new Date() | |
date.setTime date.getTime() - date.getTime() | |
expDate = date.toUTCString() | |
document.cookie = key + "=0; expires=" + expDate + "; path=/" | |
return true | |
else | |
return false | |
deleteCookies = -> | |
cookies = Object.keys readCookies() | |
for cookie in cookies | |
deleteCookie cookie | |
return true |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Generated by CoffeeScript 2.7.0 | |
// Credit: https://www.w3schools.com/js/js_cookies.asp | |
var deleteCookie, deleteCookies, readCookie, readCookies, updateCookie, writeCookie; | |
writeCookie = function(key, value, expDays = 365) { | |
var date, expDate, expTime; | |
if (String(key)) { | |
date = new Date(); | |
expTime = 1000 * 60 * 60 * 24 * expDays; | |
date.setTime(date.getTime() + expTime); | |
expDate = date.toUTCString(); | |
document.cookie = key + "=" + value + "; expires=" + expDate + "; path=/"; | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
readCookie = function(key) { | |
var cookies; | |
cookies = readCookies(); | |
if (key in cookies) { | |
return cookies[key]; | |
} else { | |
return null; | |
} | |
}; | |
readCookies = function() { | |
var cookie, cookies, i, len, ref; | |
cookies = {}; | |
ref = document.cookie.split(";"); | |
for (i = 0, len = ref.length; i < len; i++) { | |
cookie = ref[i]; | |
if (cookie) { | |
cookie = decodeURIComponent(cookie).split("="); | |
cookies[cookie[0].trim()] = cookie[1].trim(); | |
} | |
} | |
return cookies; | |
}; | |
updateCookie = function(key, value, expDays = 365) { | |
var cookie; | |
cookie = readCookie(key); | |
if (cookie !== null) { | |
return writeCookie(key, value, expDays); | |
} else { | |
return false; | |
} | |
}; | |
deleteCookie = function(key) { | |
var cookie, date, expDate; | |
cookie = readCookie(key); | |
if (cookie !== null) { | |
date = new Date(); | |
date.setTime(date.getTime() - date.getTime()); | |
expDate = date.toUTCString(); | |
document.cookie = key + "=0; expires=" + expDate + "; path=/"; | |
return true; | |
} else { | |
return false; | |
} | |
}; | |
deleteCookies = function() { | |
var cookie, cookies, i, len; | |
cookies = Object.keys(readCookies()); | |
for (i = 0, len = cookies.length; i < len; i++) { | |
cookie = cookies[i]; | |
deleteCookie(cookie); | |
} | |
return true; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment