0

How do I load multiple variables from one cookie file?

I have tried this:

if (variable1 != null) {
    variable1 = getCookie("variable1");
    variable2 = getCookie("variable2");
    variable3 = getCookie("variable3");
} else {
    variable1 = 0;
    variable2 = 0;
    variable3 = 0;
}

but that doesn't seem to work.

And this is how the getCookie function looks like:

function getCookie(cname) {
    var name = cname + "=";
    var ca = document.cookie.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);
        }
    }
}
Roberto Caboni
  • 7,252
  • 10
  • 25
  • 39
  • Welcome to Stack Overflow. Please take the [tour](https://stackoverflow.com/tour), read about what's [on-topic](https://stackoverflow.com/help/on-topic), and read [How to Ask a Good Question](https://stackoverflow.com/help/how-to-ask). In particular, please format your code (indentation) and always include a programming language tag (if it's about code). – Ivo Mori Jul 11 '20 at 13:31
  • https://stackoverflow.com/questions/14573223/set-cookie-and-get-cookie-with-javascript – Arpan Sarkar Jul 11 '20 at 19:52

0 Answers0