Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Delete cookies via JavaScript [duplicate]

Possible Duplicate:
Javascript cookie delete

Please let me know how to delete specific cookie (I have the name of the cookie its enough..).

I really dont have an idea (I dont know JS well...) and when I searched in Google I didn't find a good solution.

Thank you.

EDIT: If I can't delete the cookie - let me know how to change the value to "" (empty..), its ok too.

like image 983
Luis Avatar asked Dec 07 '22 01:12

Luis


2 Answers

Stolen from here:

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

This sets the cookie value to nothing (name + '=;) and sets the expiration time to the past (expiring it immediately).

like image 68
Oded Avatar answered Dec 19 '22 09:12

Oded


Try this (adapted from here):

function DeleteCookie(name, path, domain) 
{
    path = (path ? ";path=" + path : "");
    domain = (domain ? ";domain=" + domain : "");
    var expiration = "Thu, 01-Jan-1970 00:00:01 GMT";

    document.cookie = name + "=" + path + domain + ";expires=" + expiration;
}

Essentially, you delete cookies by setting their expiration date to a date guaranteed to be in the past.

like image 25
Tim S. Van Haren Avatar answered Dec 19 '22 10:12

Tim S. Van Haren