Client-side JavaScript cannot delete cookie written by ASP.NET

  • Thread starter Thread starter Henri
  • Start date Start date
H

Henri

Hi,

My page saves user's login on user's computer using a cookie:

Dim expires As Date = DateTime.Now.AddMonths(1)
Dim cookie As New HttpCookie("login", loginValue)
cookie.Expires = expires
Response.Cookies.Add(cookie)

On the same page, user can click on a button to remove this cookie by
clicking on a button, which calls a client-side JavaScript function:

document.cookie = "login=; expires=Thu, 01-Jan-70 00:00:01 GMT";

This doesn't work. Why?
Thanks
 
FIXED!

It seems that the cookie string used in JavaScript to delete it must match
EXACTLY the cookie string sent by ASP.NET to save it:

as ASP.NET sends:
login=myloginvalue; expires=gmtdate; path=/

You have to write the following JavaScript to delete the cookie:

var expires = new Date();
expires.setUTCFullYear(expires.getUTCFullYear() - 1);
document.cookie = 'login=; expires=' + expires.toUTCString() + '; path=/';

It works both on IE and Mozilla!

Henri
 
Back
Top