Skip to content

Instantly share code, notes, and snippets.

@thinkclay
Created November 8, 2012 22:03
Show Gist options
  • Save thinkclay/4042032 to your computer and use it in GitHub Desktop.
Save thinkclay/4042032 to your computer and use it in GitHub Desktop.
Cookie CRUD in JS
/**
* Custom Cookie Support for JS
*
* There aren't very many great ways of interacting with Cookies
* in either native JS or jQuery without the use of plugins
* so this snippet aims to give you quick and easy access
* to manipulate your cookies with native JS
*
* @author Clay McIlrath
* @link http://thinkclay.com/technology/add-edit-delete-cookies-with-javascript
* @copyright 2012 Clay McIlrath
* @license The MIT License (MIT)
*/
var Cookies = {
// Initialize by splitting the array of Cookies
init: function () {
var allCookies = document.cookie.split('; ');
for (var i=0;i<allCookies.length;i++) {
var cookiePair = allCookies[i].split('=');
this[cookiePair[0]] = cookiePair[1];
}
},
// Create Function: Pass name of cookie, value, and days to expire
create: function (name,value,days) {
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();
}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
this[name] = value;
},
// Erase cookie by name
erase: function (name) {
this.create(name,'',-1);
this[name] = undefined;
}
};
// global creation for access using window.Cookies.myCookieName
window.cookies = Cookies.init();
@killerwolf
Copy link

Where is the READ and UPDATE part of the CRUD

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment