Instantly share code, notes, and snippets.
Created
November 15, 2011 14:25
-
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 jackfuchs/1367196 to your computer and use it in GitHub Desktop.
Cookie Handling
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
/** | |
* Cookie Handling | |
* To easily handle kinds of client side Cookie manipulations | |
* | |
* [@param] object opts - options | |
* | |
* @Author: Axel Jack Fuchs (Cologne, Germany) | |
* @Date: 11-15-2011 15:35 | |
* @Updated: 11-16-2011 10:18 | |
* | |
* Example: | |
* - var Cookies = new Cookies({debug: true, prefix:'bookmark_'}); // Constructs the Cookies Object | |
* - Cookies.set('foo', 'bar'); // Sets a Cookie named 'bookmark_foo' with value 'bar' | |
* - Cookies.get('foo'); // Returns the Cookie named 'bookmark_foo' | |
* - Cookies.getAll(); // Returns all 'bookmark_' prefixed Cookies as object | |
* - Cookies.getPrefixed('bookmark_', 'counter_'); // Returns all Cookies prefixed 'bookmark_' or 'counter_' | |
* - Cookies.delete('foo'); // Removes the Cookie named 'bookmark_foo' | |
* - Cookies.delete(true); // Removes all 'bookmark_' prefixed Cookies | |
* | |
*/ | |
var Cookies = function(opts) { | |
this.cookies = {}; | |
this.support = false; | |
/* Options */ | |
for (i in opts) this[i] = opts[i]; | |
// Check for cookie support | |
this.support = (navigator.cookieEnabled || this.get('testcookie')); | |
if (!this.support && typeof navigator.cookieEnabled == 'undefined') { | |
this.set('testcookie'); | |
this.support = this.get('testcookie'); | |
this.delete('testcookie'); | |
} | |
/** | |
* set - Sets a new cookie | |
* | |
* @param string - cookie name | |
* [@param] string - cookie value | |
* [@param] object - further parameters | |
* - expire: expiration date as string or false for a persistant cookie | |
* eg. this.set('k', 'v', {expire: 'Sat, 26 Jul 1997 05:00:00 GMT'}); | |
* | |
* return void | |
*/ | |
this.set = function(k, v, params) { | |
var cookie = [ | |
(k.substring(0, this.prefix.length) == this.prefix ? '' : this.prefix) + k +'='+ (v ? encodeURIComponent(v) : ''), | |
'path=/' | |
]; | |
// Transfers the given params | |
var param, i; | |
for (i in params) { | |
param = params[i]; | |
switch (i) { | |
case 'expire': | |
cookie.push(typeof(param) == 'string' ? 'expires='+param : (param === false ? 'expires=Thu, 31-Dec-2033 00:00:00 GMT' : '')); | |
break; | |
default: | |
cookie.push(param); | |
} | |
} | |
if(this.debug) console.log(cookie.join('; ')); | |
document.cookie = cookie.join('; '); | |
}; | |
/** | |
* get - Retrieves a cookie by its name | |
* | |
* @param string - cookie name | |
* | |
* success: return string | |
* fail: return null | |
*/ | |
this.get = function(k) { | |
var nameEQ = this.prefix + k + "=", | |
ca = document.cookie.split(';'), | |
i; | |
for (i=0; i<ca.length; i++) { | |
var c = ca[i]; | |
while (c.charAt(0) == ' ') { | |
c = c.substring(1, c.length); | |
} | |
if (c.indexOf(nameEQ) == 0) { | |
return decodeURIComponent(c.substring(nameEQ.length, c.length)); | |
} | |
} | |
return null; | |
}; | |
/** | |
* getAll - Returns all cookies | |
* | |
* [@param, ...] string - Optional, to retrieve prefixed cookies | |
* | |
* return object | |
*/ | |
this.getAll = function() { | |
var doc_cookies = document.cookie; | |
if (doc_cookies == '') { return {}; } | |
var cookies = doc_cookies.split(';'), | |
i; | |
if (cookies.length == 0) { return false; } | |
for (i in cookies) { | |
if (!cookies.hasOwnProperty(i)) { continue; } | |
var cookie = cookies[i]; | |
cookie = cookie.split('='); | |
while (cookie[0].charAt(0) == ' ') { | |
cookie[0] = cookie[0].substring(1, cookie[0].length); | |
} | |
// Continues only on matching prefixes | |
var _continue = true; | |
if (arguments.length > 0) { | |
var argc = arguments.length, | |
argv = arguments; | |
if (argc == 1) { | |
_continue = !(argv[0] && cookie[0].substring(0, argv[0].length) != argv[0]); | |
} else { | |
for (var _i=0; _i<argc; _i++) { | |
if (argv[_i] && cookie[0].substring(0, argv[_i].length) == argv[_i]) { | |
_continue = false; | |
break; | |
} | |
} | |
} | |
} | |
if (_continue === true) { | |
if (this.debug) console.log('ignoring cookie with name: '+ cookie[0]); | |
continue; | |
} | |
if (this.debug) console.log(cookie); | |
this.cookies[cookie[0]] = (typeof cookie[1] === 'string') ? cookie[1] : ''; | |
} | |
return this.cookies; | |
}; | |
/** | |
* getPrefixed - Helper method for only retrieving accordingly prefixed Cookies | |
* | |
* @param[, @param, ...] string - Prefixes | |
* | |
* return object | |
*/ | |
this.getPrefixed = function() { | |
if(arguments.length < 1) { | |
throw new Error('Missing prefix arguments!'); | |
return {}; | |
} | |
return this.getAll.apply(this, arguments); | |
} | |
/** | |
* delete - Deletes all requested cookies or all cookies | |
* | |
* @param[, @param...] mixed - Cookies to delete | |
* - Either... | |
* (arguments) a list of cookie names as arguments (1-n) | |
* (array) an array with cookie names | |
* (bool) true to delete all cookies | |
* | |
* return void | |
*/ | |
this.delete = function() { | |
var argc = arguments.length, | |
argv = arguments, | |
arg, | |
cookies = {}, | |
i; | |
if (argc == 1) { | |
// One cookie name as string | |
if (typeof argv[0] == 'string') { | |
cookies[argv[0]] = ''; | |
// One or more cookie names as array object | |
} else if (typeof argv[0] == 'object') { | |
for (i=0; i<argv[0].length; i++) { | |
cookies[argv[0][i]] = i; | |
} | |
// All cookies | |
} else if (argv[0] === true) { | |
cookies = this.getAll(); // Rereads all cookies | |
} | |
// Multiple cookie names as arguments | |
} else if (argc > 1) { | |
for (i=0; i<argc; i++) { | |
cookies[argv[i]] = i; | |
} | |
} | |
for (i in cookies) { | |
// Continues on not matching prefixes | |
if(this.prefix && i.substring(0, this.prefix.length) != this.prefix) { continue; } | |
this.set(i, '', {expire: 'Sat, 26 Jul 1997 05:00:00 GMT'}); | |
} | |
}; | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment