JSFiddle - React, Tailwind, and code Playground
by jsumners
JavaScript
document.cookie = "cookie1=true";
document.cookie = "cookie2=One%20tough%20cookie";
document.cookie = "cookie3=false";
var cookieManager = ({
cookies: {},
deleteCookie: function(name) {
if (typeof this.cookies[name] !== "undefined") {
document.cookie = name + "=0; expires=Thu, 01-Jan-70 00:00:01 GMT";
delete this.cookies[name];
}
},
getCookie: function(name) {
return this.cookies[name];
},
setCookie: function(params) {
var p = {
name: "",
value: "",
expires: "",
path: "",
domain: "",
secure: false
}, c;
if (typeof params !== "object" || typeof params.name !== "string" || typeof params.value !== "string") {
return "Must specify an object with at least the name and value properties.";
}
for (var k in params) {
if (params.hasOwnProperty(k)) {
if (params[k] instanceof Date && k == "expires") {
p[k] = params[k].toGMTString();
} else if (k == "expires" && !isNaN(+params[k])) {
p[k] = (new Date(params[k])).toGMTString();
} else {
p[k] = params[k];
}
}
}
this.cookies[p.name] = p.value;
c = p.name + "=" + escape(p.value);
if (p.expires !== "") {
c += "; expires=" + p.expires;
}
if (p.path !== "") {
c += "; path=" + escape(p.path);
}
if (p.domain !== "") {
c += "; domain=" + escape(p.domain);
}
c += (p.secure) ? "; secure" : "";
document.cookie = c;
},
init: function() {
var documentCookies = document.cookie.split(';'),
cookieName, cookieValue, pos;
for (var i = 0, j = documentCookies.length; i < j; i++) {
pos = documentCookies[i].indexOf('=');
...