-1

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.

Community
  • 1
  • 1
KRLW890
  • 5
  • 4
  • Are you ever calling `mousePressed()`? – Barmar Sep 01 '16 at 19:41
  • The technical term is property. `x.y` means the property named `y` in the object `x`. – Barmar Sep 01 '16 at 19:41
  • **First comment:** Yes, I am calling `mousePressed()` **Second comment:** so `document.cookies` is a variable then? – KRLW890 Sep 01 '16 at 19:47
  • `document` is a variable. It contains an object, and the object has lots of properties. – Barmar Sep 01 '16 at 19:56
  • Yeah, okay. I thought so. – KRLW890 Sep 01 '16 at 19:58
  • Your code is working for me: https://jsfiddle.net/barmar/ctwv3wkb/ When I check the cookies in Developer Tools, I see `cookie1` and `cookie2`. – Barmar Sep 01 '16 at 20:01
  • Where are you getting the message "0 cookies from this site" from? – Barmar Sep 01 '16 at 20:03
  • Sorry, that wasn't a good fiddle, since it recreates the cookies every time yuo open the page. Here's one that adds the cookies only when you click on a button. https://jsfiddle.net/barmar/ctwv3wkb/1/ But when I close the page and reopen, I see the old cookies are still there before I click again. – Barmar Sep 01 '16 at 20:06
  • I'm programming using notepad (still a very novice programmer here), with an HTML header and whatnot. Here, I'll put the rest of my code in the question. – KRLW890 Sep 02 '16 at 00:49
  • I don't know nodepad, but your code works in a real browser. – Barmar Sep 02 '16 at 01:02
  • I know. I edit the file in notepad, then save it with the .html extension, in order to run it in a browser. My problem is that when I check how many cookies on the page (by clicking on the icon directly left of the URL) it recognizes the cookies, but says that they are "from other sites," then it won't save them after closing. I'll edit this into the question. – KRLW890 Sep 02 '16 at 01:18
  • Are you loading the page from a local file instead of a webserver? Cookies are part of the HTTP protocol, so you need to use a server. – Barmar Sep 02 '16 at 01:19
  • Oh, really? So I need to load it onto the internet for the cookies to work? – KRLW890 Sep 02 '16 at 01:23
  • Or run a webserver on your machine. You can use IIS or WAMP. – Barmar Sep 02 '16 at 01:24
  • I'm not quite sure what that means. Still a novice programmer. Would you recommend a tutorial of some sort? – KRLW890 Sep 02 '16 at 01:27
  • Sorry, I don't have anything to recommend. I'm sure Google can find a tutorial for you. – Barmar Sep 02 '16 at 01:29
  • Okay, thanks. I think this chain is done. – KRLW890 Sep 02 '16 at 01:36

1 Answers1

0

From information in an above comment chain (that probably went on for longer than it should have), I have been able to gather the following information on my problem:

  • My cookies weren't being recognized because it the page was being run from my local storage, without a server.
  • In order for the cookies to be recognized, a server needs to be running, either off the web or from the machine.
  • Two servers that can be run locally include IIS and WAMP.

Credit to Barmar for information.

KRLW890
  • 5
  • 4