0

I have a cookie structure something like below

name1=123abc; name2; name3=abcd1212; name4=1122hjhdff

Here I am trying to remove the name2, so that my structure will be like:

name1=123abc; name3=abcd1212; name4=1122hjhdff

I have looked into various posts which says to define past date for the specific name which you want to delete

var delete_cookie = function(name) {
document.cookie = name + '=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
};

delete_cookie('name2');

I am trying to use this, but I am not able to succeed. I also read various post saying that a 'HTTP' only cookie cannot be deleted by any scripting language.

I tried something like below and I highly doubt this will work, since I am altering document.cookie directly

function removeSelectedCookie() {
var val = " name2";
var arr = document.cookie.split(';');
var index = arr.indexOf(val);
arr.splice(index, 1);
document.cookie = arr.join(';');
}

I am not sure whether I am missing simple change here. Could someone help me on how to have this deleted or this cannot be deleted?

Sriram Chandramouli
  • 191
  • 1
  • 1
  • 10
  • Because `document.cookie` is not a plain mutable property and works via get/set, [you can only assign one cookie at a time](https://developer.mozilla.org/en-US/docs/Web/API/Document/cookie). Consider `document.cookie = name + '='`. – rishat Nov 30 '17 at 22:07
  • `I also read various post saying that a 'HTTP' only cookie cannot be deleted by any scripting language.` - correct, but it can't be read *by any scripting language* either, so that's irrelevant – Jaromanda X Nov 30 '17 at 22:09
  • @RishatMuhametshin - I am trying to get what you are saying. Cookie is getting set like that when I run my application. But, before executing certain scripts, I want 'name2' alone to be removed. So is it possible to remove a specific name from it? – Sriram Chandramouli Nov 30 '17 at 22:18

0 Answers0