Learn AspNet Learn AspNet - 3 months ago 15
jQuery Question

Delete cookies javascript

I have a cookie

console.log(document.cookie);
Clickme=a-6,a-7,a-8,a-9,a-10,a-17,a-8,


I want to delete this cookie, I have tried following things but they aren't working

document.cookie = "Clickme=; max-age = -1;"
document.cookie = Clickme+"=; expires=Thu, 01 Jan 1970 00:00:01 GMT;"
document.cookie = "Clickme=; expires=Thu, 01 Jan 1970 00:00:00 UTC";

Answer

I use the following to remove a cookie.

function deleteCookie(name) {
  var domain = location.hostname,
      path = '/'; // root path

  document.cookie = [
    name, '=',
    '; expires=' + new Date(0).toUTCString(),
    '; path=' + path,
    '; domain=' + domain
  ].join('');
}

deleteCookie('Clickme');

Using an array helps me see the parts separately, and using Date(0).toUTCString() guarantees I've got the correct date.