3

I'm using the following functions to create and delete cookies. This has worked great in the past, but recently I started debugging via localhost and I have noticed the deleteCookie function is no longer working.

function setCookie(cname, cvalue, exsecs) {
    let expires = "";
    if (exsecs) {
        var d = new Date();
        d.setTime(d.getTime() + (exsecs * 1000));
        expires = "expires="+ d.toUTCString();
    } else {
        expires = "expires=Fri, 31 Dec 9999 23:59:59 GMT"
    }
    document.cookie = cname + "=" + cvalue + ";" + expires + ";path=/";
}

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

As you can see, it's pretty straight forward. Is there a better way to "delete" cookies that works locally? Or is there some configuration change I can make?

Niklas Higi
  • 2,188
  • 1
  • 14
  • 30
  • 1
    Something very similar -- https://stackoverflow.com/a/23995984/1745073 – blueren Nov 29 '17 at 17:12
  • Looks like that works. But since that wasn't the specific question over there (localhost is never mentioned), I think this should probably stay open. Thoughts? –  Nov 29 '17 at 17:16

1 Answers1

2

Leaving this as a possible answer until a more solid answer with relavent explanation comes by:

function deleteCookie(name) {
  document.cookie = name +'=; Path=/; Expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

Specifying the Path attribute seems to work for other's who faced similar problems.

Source (although there is no mention of it being specific to localhost)

blueren
  • 2,730
  • 4
  • 30
  • 47