MLDev
MLDev

Reputation: 1277

Javascript, creating multiple cookie files

Is there any way to create multiple cookie files? I'm not asking about adding multiple keys to the same file but to create completely separate cookie files. Every time I try something like below, the cookie file is replaced rather than creating a new file:

document.cookie="name=Johe2; expires=Thu, 18 Dec 2017 12:00:00 UTC";
document.cookie="name=Johe; expires=Thu, 18 Dec 2017 12:00:00 UTC";

Example: As you can see below the site adobe.com has 4 cookies on my computer. I can only create 1 using the code above: enter image description here

Upvotes: 1

Views: 150

Answers (1)

adeneo
adeneo

Reputation: 318302

You can create as many cookies as you want to, but the keys have to be different.

document.cookie is a bit strange, as it can be treated as a string, but it's not really a string, there's an object behind it that stores the keys and values, so creating two different cookies is done with two different keys.

If you use the same key, the value is naturally overwritten, as that's the way you'd change the cookie values

document.cookie = "name1=Johe2; expires=Thu, 18 Dec 2017 12:00:00 UTC";
document.cookie = "name2=Johe; expires=Thu, 18 Dec 2017 12:00:00 UTC";

creates two cookies with the keys name1 and name2

Upvotes: 2

Related Questions