Learn AspNet
Learn AspNet

Reputation: 1602

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";

Upvotes: 1

Views: 2156

Answers (3)

Motombo
Motombo

Reputation: 1787

See this Delete cookie

You can use this function

function removeItem(sKey, sPath, sDomain) {
    document.cookie = encodeURIComponent(sKey) + 
                  "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + 
                  (sDomain ? "; domain=" + sDomain : "") + 
                  (sPath ? "; path=" + sPath : "");
}

removeItem("cookieName");

In angular you can use $cookies.remove as well, if that helps.

Upvotes: 0

Bhushan
Bhushan

Reputation: 46

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

Hope it works.

Upvotes: 0

evolutionxbox
evolutionxbox

Reputation: 4122

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.

Upvotes: 1

Related Questions