Reputation: 3313
I'm trying to delete all the cookies that my site uses but from reading I can't do that so I believe my only option is to change the expiration date to be in the past and let the browser do the work. I have one cookie at the moment called 'CookieDisplay'. On the page load event in asp.net I have :
Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
For Each key As String In Request.Cookies.AllKeys
Dim cookie As New HttpCookie(key)
If (Not Request.Cookies(cookie.Name.ToString) Is Nothing) Then
cookie.Expires = DateTime.Now.AddDays(-1D)
Response.Cookies.Add(cookie)
End If
Next
End Sub
This code finds the cookie for 'CookieDisplay' and changes the expiration date. However on the client side I have javascript that triggers after the page load event with the following.
function getCookie(c_name) {
var i, x, y, ARRcookies = document.cookie.split(";");
for (i = 0; i < ARRcookies.length; i++) {
x = ARRcookies[i].substr(0, ARRcookies[i].indexOf("="));
y = ARRcookies[i].substr(ARRcookies[i].indexOf("=") + 1);
x = x.replace(/^\s+|\s+$/g, "");
if (x == c_name) {
return unescape(y);
}
}
}
function checkCookie() {
var SavedData = getCookie("CookieDisplay");
if (SavedData != null && SavedData == "true") {
closeme();
}
}
The problem is the cookie is returning a value so the expiration date hasn't taken affect so the cookie doesn't expire and therefore still shows. Could it be because this cookie gets created in javascript and asp.net doesn't have access to this? Do I have to expire the cookies from javascript and asp.net depending on where the cookie got created?
Thanks
Upvotes: 0
Views: 1592
Reputation: 4077
Cookie created by Javascript and Cookie created by HTTP request do not differ. Check the domains and paths, these cookies are set for. They should be the same.
Upvotes: 1