Skip to content

Instantly share code, notes, and snippets.

@spearson
Created March 3, 2011 01:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save spearson/852169 to your computer and use it in GitHub Desktop.
Save spearson/852169 to your computer and use it in GitHub Desktop.
refactoring of w3schools functions
function getCookie(nameOfCookieToGet)
{
var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++)
{
var currentCookie = cookies[i];
var currentCookieName = currentCookie.substr(0 , currentCookie.indexOf("=")).replace(/^\s+|\s+$/g,"");
if (currentCookieName == nameOfCookieToGet)
{
var currentCookieValue = currentCookie.substr(currentCookie.indexOf("=") + 1);
return unescape(currentCookieValue);
}
}
}
function setCookie(cookieName, cookieValue, daysUntilExpiration)
{
var escapedCookieValue = escape(value);
var expirationString = "";
if (daysUntilExpiration != null)
{
var expirationDate = new Date() + daysUntilExpiration;
expirationDate.setDate(expirationDate.getDate() + daysUntilExpiration);
expirationString = "; expires=" + expirationDate.toUTCString();
}
document.cookie = cookieName + "=" + cookieValue + expirationString;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment