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

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
 
H

Henri

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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,755
Messages
2,569,537
Members
45,020
Latest member
GenesisGai

Latest Threads

Top