[Webinar] Streamline your web hosting managementRegister Today

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 555
  • Last Modified:

ASP will not delete cookie created with Javascript

Hi, I'm currently doing some maintenance on a site that uses JavaScript and plain ASP code (not Asp.Net), and I'm now very confused.

I am unable to delete a cookie for some reason.  I've tried the method they say to use everywhere:

Response.Cookies("LineItems").Expires = Date() - 1

And it does nothing.  

I'm wondering if the JavaScript cookie is the same as a regular cookie though, because I am using a cookie viewer (IECookiesView) and don't see the cookie there in any case.   Yet I can see view the cookie using the JavaScript command:

alert(document.cookie);

The cookie is set in JavaScript with a command like this:

document.cookie = "LineItems=this is what i want my cookie to be";

I'm very confused.  Is this not a cookie?  Why does it not show up in IECookiesView and undeletable with asp?


0
dparkes
Asked:
dparkes
  • 2
1 Solution
 
hieloCommented:
Have your tried something like:
Response.Cookies("LineItems").Expires=#May 10, 2002#
0
 
slamhoundCommented:
Here are two handy bits of code for messing with cookies in JavaScript. I've used this on a JavaScript/ASP site where I had both JS and ASP reading/altering the cookies fine.

Use this code and you should get well formed cookies.

<script language="JavaScript">
      function setCookie(name, value, expire) {
            document.cookie = name + "=" + escape(value) + ((expire == null) ? "" : ("; expires=" + expire.toGMTString()))
}
</script>

<SCRIPT language="JavaScript">
function getCookie(Name) {
   var search = Name + "="
   if (document.cookie.length > 0) { // if there are any cookies
      offset = document.cookie.indexOf(search)
      if (offset != -1) { // if cookie exists
         offset += search.length
         // set index of beginning of value
         end = document.cookie.indexOf(";", offset)
         // set index of end of cookie value
         if (end == -1)
            end = document.cookie.length
         return unescape(document.cookie.substring(offset, end))
      }
   }
}
</script>
0
 
dparkesAuthor Commented:
Slamhound, This gets me halfway there.

Yes it looks like my cookie was not correctly formatted.  Using a setCookie function, now it appears in the cookie explorer...

For some reason I had trouble calling the code you gave me and used this instead though, which in my case seems to work:

    function setCookie(Name, Value, Expire) {
        document.cookie = Name + "=" + escape(Value) + ";expires=" + Expire;
    }


However, the cookie is still not getting deleted using ASP code.

I've tried my original code, and hielo's above, but it's having no effect on the client side cookie.
0
 
slamhoundCommented:
If formatting has fixed one problem I'd guess that it continues to be an issue.

Can you validate in your cookie explorer that everything is formatted properly. That the name is right, the value is right and the expiration date is right once JS creates it.

The other thing to try is
Response.Cookies("LineItems") = ""
or
Response.Cookies("LineItems") = Null

This way we can be sure that your ASP is manipulating the cookie even if it can't expire it.
0

Featured Post

[Webinar] Improve your customer journey

A positive customer journey is important in attracting and retaining business. To improve this experience, you can use Google Maps APIs to increase checkout conversions, boost user engagement, and optimize order fulfillment. Learn how in this webinar presented by Dito.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now