Solved

ASP will not delete cookie created with Javascript

Posted on 2008-10-01
4
542 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

I've been trying to accomplish this for a while and it just struck me yesterday how to accomplish this task. I have done searches all over the internet looking for ways to email pages from my applications and finally I have done it!!! Every single s…
Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
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…

759 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now