Cookie trouble in IE

Discussion in 'JavaScript' started by James McMurray, Dec 16, 2007.

  1. #1
    I'm using the following line to delete cookies. It works great in Firefox, but in IE the cookie still exists, it's just lost all of its data. I can check to see if the data is there, and ignore the cookie if it isn't, but since I'm looping through my cookies when the page builds, I rather it be completely removed.

    document.cookie = name + "=noop; expires=Thu, 01-Jan-70 00:00:01 GMT" + "; path=" + path;
    Code (markup):
    Is there a better way to destroy a cookie in IE?
     
    James McMurray, Dec 16, 2007 IP
  2. godkillah

    godkillah Guest

    Messages:
    34
    Likes Received:
    1
    Best Answers:
    0
    Trophy Points:
    0
    #2
    you could use these functions to work with cookies, they should be compatible with all browsers
    function setCookie(name, value, expires) {
    var deCookie = name + "=" + escape(value);
    if(expires){
    expires= expires.toGMTString();
    deCookie += "; expires=";
    deCookie += expires;
    }
    document.cookie = deCookie;
    }
    function getCookie(name) {
    var dc = document.cookie;
    var prefix = name + "=";
    var begin = dc.indexOf("; " + prefix);
    if (begin == -1) {
    begin = dc.indexOf(prefix);
    if (begin != 0) return false;
    }else begin += 2;
    var end = document.cookie.indexOf(";", begin);
    if (end == -1)
    end = dc.length;
    return unescape(dc.substring(begin + prefix.length, end));
    }
    function delCookie(name){
    expires = new Date(1807,1,1);
    expires = expires.toGMTString();
    deCookie = name
    deCookie += "=delete; expires=";
    deCookie += expires;
    document.cookie = deCookie;
    }
    Code (markup):
     
    godkillah, Jan 6, 2008 IP