Search code examples
javascriptcookiesjoinsplitsetcookie

get and set cookie values in array position


OK, I have the logic, but unsure how to write to specific positions in a cookie that are separated with a pipe, |

For example - take a cookie with 0|0|0|0

if global variable is set to Y, then write the values c and d to the 2nd & 3rd position(assuming array) if the cookie exists - if the cookie doesn't exist then create a cookie with 0|0|c|d

if the global variable is null then write the values a and b to the 0 and 1st position if the cookies exists - if the cookie doesn't exist then create a cookie with a|b|0|0

I understand getting a cookie, and splitting the cookie to get a value, but unsure how to write to specific positions. I'm assuming using "join".

function createCookie(name,value,days) {
    if (days) {
        var date = new Date();
        date.setTime(date.getTime()+(days*24*60*60*1000));
        var expires = "; expires="+date.toGMTString();
    }
    else var expires = "";
    document.cookie = name+"="+value+expires+"; path=/";
}

function readCookie(name) {
    var nameEQ = name + "=";
    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,c.length);
        if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
    }
    return null;
}

function eraseCookie(name) {
    createCookie(name,"",-1);
}

Solution

  • Assuming your desired cookie value was named "pipes" and your variable was named globalVar, you could do something like this:

    var val = readCookie("pipes") || "";
    var valArray = val.split("|");
    if (valArray.length >= 4) {
        if (globalVar == "Y") {
            valArray[2] = 'c';
            valArray[3] = 'd';
        } else if (globalVar == null) {
            valArray[0] = 'a';
            valArray[1] = 'b';
        }
        val = valArray.join("|");
    } else {
        // fully formed cookie value didn't exist
        if (globalVar == "Y") {
            val = "0|0|c|d";
        } else {
            val = "a|b|0|0";
        }
    }
    createCookie("pipes", val, 365);
    

    I must say that storing this piped value in the cookie is very inconvenient and requires more code than is probably required to just store and retrieve multiple values from a cookie.

    From your comments, it sounds like the data is storeID|storeLocation and you have two of those. I'd suggest you just do this:

    var store1 = readCookie("store1");    // "1640|Jacob's Point"
    var store2 = readCookie("store2");    // "2001|Fred's Point"
    

    Or, if you want to make a function, you can do this:

    function readStoreInfo(item) {
        var info = readCookie(item);
        if (info) {
            var data = info.split("|");
            return({id: data[0], name: data[1]});
        }
        return(null);
    }
    
    function writeStoreInfo(item, obj) {
        var val = obj.id + "|" + obj.name;
        createCookie(item, val, 365);
    }
    

    So, to read the data for store1, you would just call:

    var store1Info = readStoreInfo("store1");
    store1Info.id = 123;
    writeStoreInfo("store1", store1Info);
    

    You can then read and write the data for store1 and store2 independently.