View Full Version : Delete cookies with Javascript

09-06-2010, 08:58 AM

I am working on a .net basket where I save the info for the products in the basket etc in a cookie.

Now I have an html button on the basket page where I want to be able to delete the product (Cookie).

I am using

<script type="text/javascript">
function delete_cookie ( cookie_name )
expires = Now();
document.cookie = cookie_Name+"= ;expires="+expires.toGMTString();

</script>But I get an error that says the cookie is not delcared? But it does exist because I retrive the name of the cookie when looping through the cookie list.

What am I doing wrong here?

Philip M
09-06-2010, 09:20 AM
<script type = "text/javascript">
function delete_cookie(cookie_name) {
document.cookie = cookie_name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
</script>Set the expiry to the start of epoch time (0) rather than now (this moment) in case the date on the user's computer is set incorrectly.

Javascript is case sensitive. cookie_name is not the same as cookie_Name. Hence your error message.

He thought he saw a Rattlesnake
That questioned him in Greek:
He looked again, and found it was
The Middle of Next Week.
"The one thing I regret," he said,
"Is that it cannot speak!"
- Lewis Carroll

09-06-2010, 09:41 AM

Thanks for the reply

I have added your code exaclty but still get the error: Error: 'ProductOne' is undefined

I have no idea what is causing this.

Philip M
09-06-2010, 11:56 AM
Are you sure that the name of the cookie is ProductOne exactly? And that the path (if any) is correct (same as in the previously set cookie)?


should delete the cookie.

09-06-2010, 12:34 PM
Yup, I get the string from the loop and pass it into the method directly

09-06-2010, 01:08 PM
This is where I save the cookies:

Response.Cookies["ProductOne"]["ID"] = lblProdID.Text;
Response.Cookies["ProductOne"]["Name"] = lblProdName.Text;
Response.Cookies["ProductOne"]["Description"] = lblProdDescription.Text;

And this is where I loop through the cookies:

StringBuilder sb = new StringBuilder();

int loop1, loop2;
HttpCookieCollection MyCookieColl;
HttpCookie MyCookie;

MyCookieColl = Request.Cookies;

// Capture all cookie names into a string array.
String[] arr1 = MyCookieColl.AllKeys;

// Grab individual cookie objects by cookie name.
for (loop1 = 0; loop1 < arr1.Length; loop1++)
MyCookie = MyCookieColl[arr1[loop1]];
//Response.Write("Product Name: " + MyCookie.Name + "<br>");
sb.Append("Product: ");

string strCookieName = MyCookie.Name;

//Grab all values for single cookie into an object array.
String[] arr2 = MyCookie.Values.AllKeys;

//Loop through cookie Value collection and print all values.
for (loop2 = 0; loop2 < arr2.Length; loop2++)
string strCookie = Server.HtmlEncode(arr2[loop2]);
//Response.Write(Server.HtmlEncode(arr2[loop2]) + " " + Request.Cookies[strCookieName][strCookie].ToString() + "<br>");

sb.Append("<tr><td valign='top' width='160'>");
sb.Append("</td><td>" + " " + Request.Cookies[strCookieName][strCookie].ToString());

sb.Append("<INPUT TYPE='button' NAME='Delete' Value='Delete' onClick='delete_cookie(" + strCookieName + ")'>");//delete button

Philip M
09-06-2010, 02:23 PM
I am afraid that I am unable to identify your problem, but if you can set a cookie then you must be able to delete it by overwriting it with another cookie of the same name with an expiry date in the past.

Have you tried
function delete_cookie(cookie_name) {
alert(cookie_name); // what do you get here?