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 8 of 8
  1. #1
    New Coder
    Join Date
    Sep 2006
    Location
    USA
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Deleting/Erasing a Cookie

    Okay, I'm beside myself on this one. I have checked all over the web and I can't seem to find any info on deleting/erasing cookies. The most I have found is instructions for setting the cookie expiration date to a past date.

    This works for removing the cookie value but the cookie name remains until the browser is restarted. Is there a way to completely delete/erase a cookie as a function?

    I have a page that records [in a cookie] a visitor's choice of a skin for a table. They also have the option of resetting the skin to the default [by deleting/erasing the cookie] and checking the cookie to verify it is present/gone. This is why I need it to go away completely when the visitor kills it.

    Any thoughts on this would be greatly appreciated.
    • Jesus called. He says He misses you.

  • #2
    Regular Coder
    Join Date
    Jan 2007
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    the only method I know is the past time method which you have already meantioned, why not just set the cookie to the default value?
    Matthew Bagley
    Paramiliar Design Studios
    Website Design | Website Development | Search Engine Optimisation (SEO)

  • #3
    Regular Coder
    Join Date
    Jun 2002
    Location
    Adirondacks
    Posts
    516
    Thanks
    4
    Thanked 4 Times in 4 Posts

  • #4
    New Coder
    Join Date
    Sep 2006
    Location
    USA
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    To neomaximus2k:
    I may have to just set the cookie to the default and be done with it.

    My intention was to be forthright with visitors and allow them to see the cookie (selectedSkin="whatever_skin.gif") and verify that the cookie had been erased when they reset the skin.

    This is proving much more difficult though. I am still open if anyone has further insights as to how browsers handle cookies.

    To FJbrian:
    Thanks for the link but I already went through that documentation. It didn't work correctly in IE 6 [imagine that]. I'll check it again though. Maybe I did something wrong.
    • Jesus called. He says He misses you.

  • #5
    Regular Coder
    Join Date
    Jan 2007
    Posts
    213
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Well I went into one of my old scripts that used javascript for cookies and this is how i used to remove the cookies

    Code:
    function eraseCookie() {
    	createCookie(CookieName,"",-1);
    }
    change cookiename for the name of your cookie and try that
    Matthew Bagley
    Paramiliar Design Studios
    Website Design | Website Development | Search Engine Optimisation (SEO)

  • #6
    New Coder
    Join Date
    Sep 2006
    Location
    USA
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Still no luck but I think I am narrowing the problem down. My IE browsers seem to be having problems with the cookie [retention, deleting, etc.] but it appears that my Mozilla browsers are doing better.

    I need to do some more debugging and error checking throughout a series of browsers and versions.

    Stay tuned. I'll post back when I have something.

    Thanks for all the help.
    • Jesus called. He says He misses you.

  • #7
    Senior Coder
    Join Date
    Nov 2006
    Posts
    1,000
    Thanks
    0
    Thanked 0 Times in 0 Posts
    Setting the expire date prior to the current date works for me to delete the cookie immediately in IE7 and FF2 in my test. What is your cookie code?

    david_kw

  • #8
    New Coder
    Join Date
    Sep 2006
    Location
    USA
    Posts
    44
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am coding to include older browsers also and that is where some of the problem was. Also, as I mentioned, There were inconsistencies between browser engines. Basically it depended on what browser I was using [thus the need to cross-test scripts].

    I started combining different scripts and found most worked on one browser engine or another. After a lot of testing, combining and refining here's a stable cross-browser answer with a delete verification [confirm]:

    Code:
    function skinReset(){
    if (confirm("You are about to reset your skin selection to the default. Do you want to continue?")) {
    delete_cookie("selectedSkin");
    alert("The skin has been reset and the cookie removed. Some browsers hold the remenants of the cookie until the browser is restarted.");
    }else {
    return null;
    } 
    }
     
    function delete_cookie (cookie_name){
    var cookie_date = new Date ( ); // current date & time
    cookie_date.setTime( cookie_date.getTime() - 1 );
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString(); // IE delete cookie
    document.cookie = cookie_name + '; expires=Sun, 31-Dec-2000 24:00:00 GMT; path=/'; // FF/Moz delete cookie
    }
    Hopes this helps others facing similar probs.
    • Jesus called. He says He misses you.


  •  

    Posting Permissions

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