Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 7 of 7
  1. #1
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Delete cookies with Javascript

    Hi

    I am working on a .net basket where I save the info for the products in the basket etc in a cookie.

    Now I have an html button on the basket page where I want to be able to delete the product (Cookie).

    I am using

    Code:
    <script type="text/javascript">
    function delete_cookie ( cookie_name )
    {
    expires = Now();
    document.cookie = cookie_Name+"= ;expires="+expires.toGMTString();
    }

    </script>But I get an error that says the cookie is not delcared? But it does exist because I retrive the name of the cookie when looping through the cookie list.

    What am I doing wrong here?

  • #2
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Code:
    <script type = "text/javascript">
    function delete_cookie(cookie_name) {
    document.cookie = cookie_name + '=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
    }
    </script>
    Set the expiry to the start of epoch time (0) rather than now (this moment) in case the date on the user's computer is set incorrectly.

    Javascript is case sensitive. cookie_name is not the same as cookie_Name. Hence your error message.


    He thought he saw a Rattlesnake
    That questioned him in Greek:
    He looked again, and found it was
    The Middle of Next Week.
    "The one thing I regret," he said,
    "Is that it cannot speak!"
    - Lewis Carroll

  • #3
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts

    still getting error

    Hi

    Thanks for the reply

    I have added your code exaclty but still get the error: Error: 'ProductOne' is undefined

    I have no idea what is causing this.

  • #4
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    Are you sure that the name of the cookie is ProductOne exactly? And that the path (if any) is correct (same as in the previously set cookie)?

    delete_cookie("ProductOne");

    should delete the cookie.

  • #5
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Yup, I get the string from the loop and pass it into the method directly

  • #6
    New to the CF scene
    Join Date
    Sep 2010
    Posts
    4
    Thanks
    0
    Thanked 0 Times in 0 Posts
    This is where I save the cookies:
    Code:
    Response.Cookies["ProductOne"]["ID"] = lblProdID.Text;
            Response.Cookies["ProductOne"]["Name"] = lblProdName.Text;
            Response.Cookies["ProductOne"]["Description"] = lblProdDescription.Text;
    And this is where I loop through the cookies:
    Code:
    StringBuilder sb = new StringBuilder();
    
            int loop1, loop2;
            HttpCookieCollection MyCookieColl;
            HttpCookie MyCookie;
    
            MyCookieColl = Request.Cookies;
    
            // Capture all cookie names into a string array.
            String[] arr1 = MyCookieColl.AllKeys;
    
            // Grab individual cookie objects by cookie name.
            for (loop1 = 0; loop1 < arr1.Length; loop1++)
            {
                MyCookie = MyCookieColl[arr1[loop1]];
                //Response.Write("Product Name: " + MyCookie.Name + "<br>");
                sb.Append("<table>");
                sb.Append("<tr><td>");
                sb.Append("<table>");
                sb.Append("<tr><td>");
                sb.Append("Product: ");
                sb.Append("</td><td>");
                sb.Append(MyCookie.Name);
                sb.Append("</td></tr>");
    
                string strCookieName = MyCookie.Name;
    
                //Grab all values for single cookie into an object array.
                String[] arr2 = MyCookie.Values.AllKeys;
    
                //Loop through cookie Value collection and print all values.
                for (loop2 = 0; loop2 < arr2.Length; loop2++)
                {                
                    string strCookie = Server.HtmlEncode(arr2[loop2]);
                    //Response.Write(Server.HtmlEncode(arr2[loop2]) + " " + Request.Cookies[strCookieName][strCookie].ToString() + "<br>");
    
                    sb.Append("<tr><td valign='top' width='160'>");
                    sb.Append(Server.HtmlEncode(arr2[loop2]));
                    sb.Append("</td><td>" + " " + Request.Cookies[strCookieName][strCookie].ToString());
                    sb.Append("</td></tr>");
    
                }
                sb.Append("</table>");
                sb.Append("</td><td>");
                sb.Append("<INPUT TYPE='button' NAME='Delete' Value='Delete' onClick='delete_cookie(" + strCookieName + ")'>");//delete button
                sb.Append("</td></tr>");

  • #7
    Supreme Master coder! Philip M's Avatar
    Join Date
    Jun 2002
    Location
    London, England
    Posts
    18,308
    Thanks
    203
    Thanked 2,563 Times in 2,541 Posts
    I am afraid that I am unable to identify your problem, but if you can set a cookie then you must be able to delete it by overwriting it with another cookie of the same name with an expiry date in the past.


    Have you tried
    function delete_cookie(cookie_name) {
    alert(cookie_name); // what do you get here?


  •  

    Tags for this Thread

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •