Recently, I've been trying to figure out how to implement cookies into a JS/HTML program, but for whatever reason, it just wont. work. I looked at w3schools' tutorial and the accepted answer from How do I create and read a value from cookie?. I edit the file in notepad (I know, extremely primitive editing software), then save the file with the .html extension, in order to run it in a browser. But when I do, the browser recognizes the cookies, but says that they are from other sites." These cookies are not saved after closing and reopening the page, and cannot be loaded using document.cookies
My code is
var canvas = document.getElementById("canvas");
var processing = new Processing(canvas, function(processing) {
processing.size(400, 400);
processing.background(0xFFF);
with(processing) {
var createCookie = function(name, value, days) {
var expires;
if (days) {
var date = new Date();
date.setTime(date.getTime() + (days * 24 * 60 * 60 * 1000));
expires = "; expires=" + date.toGMTString();
} else {
expires = "";
}
document.cookie = name + "=" + value + expires + "; path=/";
};
function getCookie(c_name) {
if (document.cookie.length > 0) {
c_start = document.cookie.indexOf(c_name + "=");
if (c_start != -1) {
c_start = c_start + c_name.length + 1;
c_end = document.cookie.indexOf(";", c_start);
if (c_end == -1) {
c_end = document.cookie.length;
}
return unescape(document.cookie.substring(c_start, c_end));
}
}
return "";
};
createCookie("startup", "true", 365 * 3);
var cookieNum = 1;
mousePressed = function() {
createCookie("cookie" + cookieNum, "value", 365 * 3);
cookieNum++;
};
}
if (typeof draw !== 'undefined') processing.draw = draw;
});
<canvas id="canvas"></canvas>
Warning: code might not run with new edits.