Skip to content

Instantly share code, notes, and snippets.

@loickreitmann
Created December 6, 2010 22:21
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 loickreitmann/731092 to your computer and use it in GitHub Desktop.
Save loickreitmann/731092 to your computer and use it in GitHub Desktop.
JS function to get or set a cookie
/**
* doCookie: function to get or set a cookie
* @param name (string): cookie name
* @param value (string - optional): cookie value to set
* @param options (object - optional): possible options are
* - expires: (number of days | proper datetime string)
* - path: (string)
* - domain: (string)
*/
function doCookie(name, value, options) {
var expires = '', date, path, domain, cookieValue = null, cookie, cookies, i;
if (typeof value !== 'undefined') {
/* value provided, set cookie */
options = options || {};
if (value === null) {
value = '';
options.expires = -1;
}
if (options.expires && (typeof options.expires === 'number' || options.expires.toUTCString)) {
if (typeof options.expires === 'number') {
date = new Date();
date.setTime(date.getTime() + (options.expires * 24 * 60 * 60 * 1000));
} else {
date = options.expires;
}
expires = '; expires=' + date.toUTCString();
}
path = options.path ? '; path=' + (options.path) : '';
domain = options.domain ? '; domain=' + (options.domain) : '';
document.cookie = [name, '=', encodeURIComponent(value), expires, path, domain].join('');
} else {
/* no value, get cookie */
if (document.cookie && document.cookie !== '') {
cookies = document.cookie.split(';');
for (i = 0; i < cookies.length; i += 1) {
// trim value
cookie = cookies[i].replace(/^[\s]+/, '').replace(/[\s]+$/, '');
if (cookie.indexOf(name + '=') === 0) {
cookieValue = decodeURIComponent(cookie.substring(name.length + 1));
break;
}
}
}
return cookieValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment