Skip to content

Instantly share code, notes, and snippets.

@fuzzyfox
Created September 2, 2010 18:36
Show Gist options
  • Save fuzzyfox/562694 to your computer and use it in GitHub Desktop.
Save fuzzyfox/562694 to your computer and use it in GitHub Desktop.
JavaScript: JSON flavoured cookies
/*
This is a cookiemonster... it is a cookie control that uses JSON to store
information and make it easy to use.
*/
var cookiemonster = {};
/**
* This function creates the cookie.
*
* @param flavour string The name of the cookie
* @param ingredients json The data to be stored in the cookie.
* @param bestBefore int The number of hours to keep the cookie
* @param jar string The path for the cookie, default is '/'
*/
cookiemonster.bake = function(flavour, ingredients, bestBefore, jar)
{
//set the bestBefore to correct format
if(bestBefore)
{
var date = new Date();
date.setTime(date.getTime() + (bestBefore * 60 * 60 * 1000));
bestBefore = '; expires=' + date.toUTCString();
}
else
{
bestBefore = '';
}
//stringify the ingredients and then encode them.
ingredients = (typeof ingredients == '')? encodeURIComponent('{}')
: encodeURIComponent(JSON.stringify(ingredients));
//check that a cookiejar exists and make one before baking the cookie
jar = (typeof jar == 'undefined')? '; path=/' : '; path=' + jar;
//bake the cookie
document.cookie = flavour + '=' + ingredients + bestBefore + jar;
//check oven
return !!(typeof cookiemonster.taste(flavour) === 'object');
}
/**
* This function reads the cookie and returns its contents
*
* @param flavour string The name of the cookie to read
* @returns object The json object that the cookie contains if it is found, false if it is not
*/
cookiemonster.taste = function(flavour)
{
/*
set the return value to false so that if we don't find the cookie we want
it is easy to tell when the function is used
*/
var ingredients = false;
//we want to search for the name of the cookie followed by an '='
flavour += '=';
//these are all the cookies we need to search to find ours
var cookies = document.cookie.split(';');
//loop through cookies till we find our cookie
for(var i = 0; i < cookies.length; i++)
{
//this is the cookie currently being checked
var cookie = cookies[i];
//trim all whitespace from the the ends of the cookie
cookie = cookie.trim();
//check the name of the cookie agains what we are looking for
if(cookie.indexOf(flavour) == 0)
{
//decode the cookie and parse the json
ingredients = JSON.parse(decodeURIComponent(cookie.substring(flavour.length, cookie.length)));
}
}
//return the results
return ingredients;
}
/**
* This function deletes a cookie.
*
* @param flavour string The name of the cookie to delete
* @returns true if the cookie is deleted, false if it is not
*/
cookiemonster.omnomnom = function(flavour)
{
//delete the cookie
cookiemonster.bake(flavour, '', -1);
//check that it is gone and report back
return (cookiemonster.taste(flavour) == false)? true : false;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment