function getCookies() {
if(!document.cookie){ return {}; }
var o = {}, i, m, key, value, c_arr = document.cookie.split(';'), re = /\s*([^=]+)=(.*)/;
for (i=0; i<c_arr.length; i++) {
m = c_arr[i].match(re);
if (m) {
name = m[1];
value = m[2];
name = unescapeCookieKey(name);
value = unescapeCookieValue(value);
o[ name ] = value;
}
}
return o;
}
function getCookie(name) {
return getCookies()[name];
}
function eraseCookie(name, path) {
path = path || '/';
name = escapeCookieKey(name);
document.cookie = name+'=; Max-Age=-99999999; path=' + path;
}
function eraseCookies(path){
for(var name in getCookies()){
eraseCookie(name, path);
}
}
function setCookie(name, value, days, path) {
var expires = "";
value = value || "";
path = path || "/";
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days*24*60*60*1000));
expires = "; expires=" + date.toUTCString();
}
name = escapeCookieKey(name);
value = escapeCookieValue(value);
document.cookie = name + "=" + value + expires + "; path=" + path;
}
function setCookie_v2(name, value, expire, path) {
name = name || "";
value = value || "";
path = path || "/";
var parts = [];
name = escapeCookieKey(name);
value = escapeCookieValue(value);
parts.push(name + "=" + value);
parts.push("path=" + path);
if (typeof expire !== 'undefined') {
date = parseDate(expire);
if (date) {
parts.push("expires=" + date.toUTCString());
}
}
document.cookie = parts.join('; ');
}
// NB: URI encode is the general accepted way of safely escaping cooky key or value.
// TODO: perhaps a better alternative can be found?
// see for example:
// https://stackoverflow.com/questions/6869866/do-i-need-to-escape-cookie-values-when-setting-from-servlet-api
function escapeCookieKey(v){
return encodeURIComponent(v);
}
function unescapeCookieKey(v){
return decodeURIComponent(v);
}
function escapeCookieValue(v){
return encodeURIComponent(v);
}
function...
Please Whitelist JSFiddle in your content blocker.
Help keep JSFiddle free for always by one of two ways:
Whitelist JSFiddle in your content blocker (two clicks)
Go PRO and get access to additional PRO features →
Join the 4+ million users, and keep the JSFiddle dream alive.
Ad-free
All ads in the editor and listing pages are turned completely off.
Use pre-released features
You get to try and use features (like the Palette Color Generator) months before everyone else.
Fiddle collections
Sort and categorize your Fiddles into multiple collections.
Private collections and fiddles
You can make as many Private Fiddles, and Private Collections as you wish!
Console
Debug your Fiddle with a minimal built-in JavaScript console.