29

I'm trying to set multiple cookies in document.cookie, but unfortunately only one is getting added.

I know there are multiple examples present on the 'Net for setting up these kind of cookies,and I followed one of them. But still I'm unable to set that out. I followed this link to set my cookie.

My Code:

   function setCookie(start_time,end_session_time,total_time,flag,count){
     var cookie_string = "start_time="+start_time;;

    if(end_session_time) {
        cookie_string +="; end_session_time="+end_session_time;
    }

    if(total_time){
        cookie_string +="; total_time="+total_time;
    }
    if(flag){
        cookie_string +="; flag="+flag;
    }
    if(count){
        cookie_string +="; count="+count;
    }

    document.cookie =cookie_string ;
    console.log(cookie_string);

    console.log("document.cookie ="+ document.cookie);
}

The Output:

cookie_string :: start_time=1369926508266; flag=1; count=1
document.cookie =start_time=1369926508266; 
1

3 Answers 3

45

Adding a cookie is performed via document.cookie = "name=value" to add multiple keys, you should perform multiple assigments

function setCookie(start_time, end_session_time, total_time, flag, count) {
    document.cookie = "start_time=" + start_time;

    if (end_session_time) {
        document.cookie = "end_session_time=" + end_session_time;
    }
    if (total_time) {
        document.cookie = "total_time=" + total_time;
    }
    if (flag) {
        document.cookie = "flag=" + flag;
    }
    if (count) {
        document.cookie = "count=" + count;
    }

    console.log("document.cookie = " + document.cookie);
}
Sign up to request clarification or add additional context in comments.

4 Comments

I didn't think this would work - because it is counterintuitive - but it does! :-O
Is there a reason for 2 semicolons on line 2 or was that a mistake?
@Jaketr00 That must have been a mistake. I've edited it.
@Brad, Yup...exactly why I googled it and ended up here :).
31

Cookies are key value pairs (with some optional additional info added on, like the expiry date). To set more than one, you just set document.cookie more than once. The ; separator is used to specify the additional info, not to add more different cookies.

2 Comments

please do provide a code sample. I believe setting the document.cookie variable the second time over-writes the first value don't it ?
It's counter-intuitive, but setting document.cookie does not overwrite already set cookies, except the one cookie with the same name, if it exists. And the way to remove a cookie, is to set its expiry date to the past. I recommend experimenting in the developer tools of your browser!
3

There you go a sample example to add, list and delete multiple cookies

<!DOCTYPE html>
<html>
<head>
<script>
var n=1;
function addCookie(){
document.cookie=n+"="+n;n++;
}

function ListCookies(){
var result = document.cookie;
document.getElementById("p").innerHTML=result;
}

function removeCookies(){
//document.cookie="";
var result = document.cookie;
var cookieArray = result.split(";");
for(var i=0;i<cookieArray.length;i++){
   var keyValArr = cookieArray[i].split("=");
   document.cookie=keyValArr[0]+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC";
}
}

</script>

</head>
<body>
<button onclick='addCookie()'>ADD COOKIE</button><br>
<button onclick='ListCookies()'>LIST COOKIES</button>
<button onclick='removeCookies()'>REMOVE COOKIES</button>
<h1>RESULT:</h1>
<p id="p"></p>
</body>
</html>

3 Comments

Is it possible to remove particular cookie? rather remove all the cookies. Assume, I added three values one after another. And I want to remove the second cookie. Can you please help @mateen
yes it is possible, just before deleting cookie check if its the cookie to be deleted. like ***************** if(yourCondition){ document.cookie=keyValArr[0]+"=; expires=Thu, 01 Jan 1970 00:00:00 UTC"; }
would you be so kind as to check this question. stackoverflow.com/questions/51693350/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.