Skip to content

Instantly share code, notes, and snippets.

@joncombe
Created March 17, 2012 09:44
Show Gist options
  • Select an option

  • Save joncombe/2057145 to your computer and use it in GitHub Desktop.

Select an option

Save joncombe/2057145 to your computer and use it in GitHub Desktop.
Jookie: a jQuery JSON cookie handler (See http://joncom.be/code/jquery-jookie/)
/*
License:
Jookie 1.0 jQuery Plugin
Copyright (c) 2009 Jon Combe (http://joncom.be)
Permission is hereby granted, free of charge, to any person
obtaining a copy of this software and associated documentation
files (the "Software"), to deal in the Software without
restriction, including without limitation the rights to use,
copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following
conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
OTHER DEALINGS IN THE SOFTWARE.
*/
(function($) {
$.Jookie = {
Data: {},
Debug: function(a) { Debug(a) },
Delete: function(a) { Delete(a) }, // delete cookie
Get: function(a,b) { return Get(a,b) }, // get a single value from a cookie
Initialise: function(a,b) { Initialise(a,b) },
Set: function(a,b,c) { Set(a,b,c) }, // set a single value to a cookie
Unset: function(a,b) { Unset(a,b) } // remove a single value from a cookie
};
// PUBLIC: show debugging information
function Debug(sName) {
var lsRegExp = /\+/g;
var sJSON = unescape(String(Extract(sName)).replace(lsRegExp, " "));
alert("Name: " + sName +
"\nLifespan: " + $.Jookie.Data[sName].iLifespan +
" minutes\nCookie Existed Prior to Init: " + $.Jookie.Data[sName].bMadeEarlier + "\n\n" +
sJSON);
}
// PUBLIC: delete a cookie
function Delete(sName) {
delete $.Jookie.Data[sName];
document.cookie = (sName + "=; expires=" + (new Date(1990, 6, 3)).toGMTString() + "; path=/");
}
// PRIVATE: extract the contents of a cookie
function Extract(sName) {
var vValue = null;
var aContents = document.cookie.split(';');
sName += "=";
// loop through cookie strings
for (var iIndex in aContents) {
var sString = aContents[iIndex];
while (sString.charAt(0) == " ") {
sString = sString.substring(1, sString.length);
}
if (sString.indexOf(sName) == 0) {
vValue = sString.substring(sName.length, sString.length);
break;
}
}
// return extracted value
return vValue;
}
// PUBLIC: retrieve a cookie's value
function Get(sName, sVariableName) {
return $.Jookie.Data[sName].oValues[sVariableName];
}
// PUBLIC: Initialise the plugin
function Initialise(sName, iLifespanInMinutes) {
if (typeof $.Jookie.Data[sName] == "undefined") {
var oRetrievedValues = {};
var bCookieExists = false;
// extract cookie value
var vCookieValue = Extract(sName);
if (vCookieValue !== null) {
oRetrievedValues = JSON.parse( unescape(String(vCookieValue).replace(/\+/g, " ")) );
bCookieExists = true;
}
// add cookie details to object
$.Jookie.Data[sName] = { iLifespan : iLifespanInMinutes,
bMadeEarlier : bCookieExists,
oValues : oRetrievedValues };
Save(sName);
}
}
// PRIVATE: write cookie to user's browser
function Save(sName) {
var sExpires = "";
if ($.Jookie.Data[sName].iLifespan > 0) {
var dtDate = new Date();
dtDate.setMinutes(dtDate.getMinutes() + $.Jookie.Data[sName].iLifespan);
sExpires = ("; expires=" + dtDate.toGMTString());
}
document.cookie = (sName + "=" +
escape(JSON.stringify($.Jookie.Data[sName].oValues)) +
sExpires + "; path=/");
}
// PUBLIC: set and save a cookie's value
function Set(sName, sVariableName, vValue) {
$.Jookie.Data[sName].oValues[sVariableName] = vValue;
Save(sName);
}
// PUBLIC: delete a single variable from a cookie
function Unset(sName, sVariableName) {
delete $.Jookie.Data[sName].oValues[sVariableName];
Save(sName);
}
})(jQuery);
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment