Solved

ASP will not delete cookie created with Javascript

Posted on 2008-10-01
4
545 Views
Last Modified: 2012-05-05
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
Comment
Question by:dparkes
  • 2
4 Comments
 
LVL 82

Expert Comment

by:hielo
ID: 22620507
Have your tried something like:
Response.Cookies("LineItems").Expires=#May 10, 2002#
0
 
LVL 10

Accepted Solution

by:
slamhound earned 500 total points
ID: 22620987
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
 
LVL 1

Author Comment

by:dparkes
ID: 22625447
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
 
LVL 10

Expert Comment

by:slamhound
ID: 22630237
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Introduction HTML checkboxes provide the perfect way for a web developer to receive client input when the client's options might be none, one or many.  But the PHP code for processing the checkboxes can be confusing at first.  What if a checkbox is…
Boost your ability to deliver ambitious and competitive web apps by choosing the right JavaScript framework to best suit your project’s needs.
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
The viewer will learn the basics of jQuery including how to code hide show and toggles. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery…

809 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question