-1

I'm trying to create a function in JavaScript that sets a cookie with a numeric value each time the page is loaded. Here is my try:

function createCookie(cookieName, cookieValue, days) {
    if (cookieName == null) {
        cookieValue = 0;
    }
    else cookieValue +=1;
    document.cookie = cookieName + "=" + cookieValue + days + "; path=/";
    console.log(cookieName + ":" + cookieValue );
}

createCookie('foo', 3, 7)

The output is:

foo:4

I'm a newbie to JavaScript. How can I amend my code to increment the value of the cookie each time the page is loaded?

Thanks!

equanimity
  • 2,371
  • 3
  • 29
  • 53
  • So read the cookie's value https://stackoverflow.com/questions/4825683/how-do-i-create-and-read-a-value-from-cookie – epascarello Nov 23 '21 at 03:15
  • @epascarello -- why do I need to read the cookie's value via a function if I'm printing the value to the console? That was my question... – equanimity Nov 23 '21 at 23:50
  • How can you increment the value in the cookie if you do not read the value that is in the cookie??? With your code you are always setting the cookie value to 4. – epascarello Nov 24 '21 at 14:48

1 Answers1

0

You can implement solution like this for cookie management:

class CookieManager {

    constructor() {}

    /**
     * Set cookie (add new or edit already added one)
     * @param name name of cookie property 
     * @param value cookie value
     * @param expireTimeInMs cookie expire time in milliseconds (1 day by default)
     */
    static set(name, value, expireTimeInMs = 24 * 36e5) {
        var d = new Date();
        d.setTime(d.getTime() + expireTimeInMs);
        var expires = "expires="+ d.toUTCString();
        document.cookie = name + "=" + value + ";" + expires + ";path=/";
    }

    /**
     * Get current cookie value 
     * @param name name of cookie property 
     * @returns value or 'null' if cookie not found
     */
    static get(name) {
        name = name + "=";
        var decodedCookie = decodeURIComponent(document.cookie);
        var ca = decodedCookie.split(';');
        for (var i = 0; i < ca.length; i++) {
          var c = ca[i];
          while (c.charAt(0) == ' ') {
            c = c.substring(1);
          }
          if (c.indexOf(name) == 0) {
            return c.substring(name.length, c.length);
          }
        }
        return null;
    }

    /**
     * Increment integer cookie value
     * @param name name of cookie property 
     */
    static inc(name) {
        var value = parseInt(this.get(name))
        if (!value || Number.isNaN(value)) value = 0;
        this.set(name, value + 1);
    }
}

So you can store & increment any int cookie like this:

CookieManager.inc(cookieName);
Xeelley
  • 1,081
  • 2
  • 8
  • 18
  • How would I display the current value of the cookie in the console? – equanimity Nov 23 '21 at 23:17
  • @equanimity you can easily access to cookie value via `get` method, so log out possible via `console.log(CookieManager.get(cookieName))` – Xeelley Nov 24 '21 at 07:30