Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7678311
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T17:37:02+00:00 2026-05-31T17:37:02+00:00

OK, I have the logic, but unsure how to write to specific positions in

  • 0

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);
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-31T17:37:03+00:00Added an answer on May 31, 2026 at 5:37 pm

    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.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have two functions that have different enough logic but pretty much the same
I have two functions whose underlying logic is the same but in one case
I am new to VBA but have previous experience with PHP programming logic and
I have some login code that works well for Firefox and other browsers, but
My app should implement login with facebook but I have noticed that every time
I have read many questions about the facebook login but until not I didnt
I have these roles:Admin, Doctor and Patient. But login information is stored in different
On a website I have implemented the login using OpenID (based on StackOverflow). But
I have just finished creating an entire login and register systsem in PHP, but
I have the business logic of an workflow-like application in a C# class library,

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.