1

I am using this function to set cookies

    function setCookie(name,value,days) {
      var expires = "";
      if (days) {
        var date = new Date();
        date.setTime(date.getTime() + (days*24*60*60*1000));
        expires = "; expires=" + date.toUTCString();
      }
      document.cookie = name + "=" + (value || "")  + expires + "; path=/";
    }

setCookie("test_cookie", "test_value", 1);

I found different ways here to delete a cookie but none of them worked for me. As example, I unsuccessfully tried this :

document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;';

I tried this code on both Firefox and chrome. The cookie remain on the browser after changing the expires value, Do you have an idea about the problem?

dens
  • 13
  • 2

1 Answers1

0

I just ran into this issue and finally solved it. Your cookie is most likely not being deleted because when you set the new value, it has to match the path and domain of the original cookie you're trying to delete.

In other words:

 document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT;path=[something];"

that "something" value needs to line up with whatever the existing cookies have set.

JS debuggers might not give you details on what the path and domain are, but it will become obvious which one you're not matching on if you look up the value of the existing cookie in your Chrome->settings or similar panel in Firefox/Safari/IE.

Let me know if that helps.

Giuseppe Canale
  • 470
  • 7
  • 15