How to delete a cookie?

后端 未结 8 1543
说谎
说谎 2020-11-22 02:08

Is my function of creating a cookie correct? How do I delete the cookie at the beginning of my program? is there a simple coding?

function createCookie(name,         


        
相关标签:
8条回答
  • 2020-11-22 02:13

    Here a good link on Quirksmode.

    function setCookie(name,value,days) {
        var expires = "";
        if (days) {
            var date = new Date();
            date.setTime(date.getTime() + (days*24*60*60*1000));
            expires = "; expires=" + date.toUTCString();
        }
        document.cookie = name + "=" + (value || "")  + expires + "; path=/";
    }
    function getCookie(name) {
        var nameEQ = name + "=";
        var ca = document.cookie.split(';');
        for(var i=0;i < ca.length;i++) {
            var c = ca[i];
            while (c.charAt(0)==' ') c = c.substring(1,c.length);
            if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length);
        }
        return null;
    }
    function eraseCookie(name) {   
        document.cookie = name+'=; Max-Age=-99999999;';  
    }
    
    0 讨论(0)
  • 2020-11-22 02:15

    To delete a cookie I set it again with an empty value and expiring in 1 second. In details, I always use one of the following flavours (I tend to prefer the second one):

    1.

        function setCookie(key, value, expireDays, expireHours, expireMinutes, expireSeconds) {
            var expireDate = new Date();
            if (expireDays) {
                expireDate.setDate(expireDate.getDate() + expireDays);
            }
            if (expireHours) {
                expireDate.setHours(expireDate.getHours() + expireHours);
            }
            if (expireMinutes) {
                expireDate.setMinutes(expireDate.getMinutes() + expireMinutes);
            }
            if (expireSeconds) {
                expireDate.setSeconds(expireDate.getSeconds() + expireSeconds);
            }
            document.cookie = key +"="+ escape(value) +
                ";domain="+ window.location.hostname +
                ";path=/"+
                ";expires="+expireDate.toUTCString();
        }
    
        function deleteCookie(name) {
            setCookie(name, "", null , null , null, 1);
        }
    

    Usage:

    setCookie("reminder", "buyCoffee", null, null, 20);
    deleteCookie("reminder");
    

    2

        function setCookie(params) {
            var name            = params.name,
                value           = params.value,
                expireDays      = params.days,
                expireHours     = params.hours,
                expireMinutes   = params.minutes,
                expireSeconds   = params.seconds;
    
            var expireDate = new Date();
            if (expireDays) {
                expireDate.setDate(expireDate.getDate() + expireDays);
            }
            if (expireHours) {
                expireDate.setHours(expireDate.getHours() + expireHours);
            }
            if (expireMinutes) {
                expireDate.setMinutes(expireDate.getMinutes() + expireMinutes);
            }
            if (expireSeconds) {
                expireDate.setSeconds(expireDate.getSeconds() + expireSeconds);
            }
    
            document.cookie = name +"="+ escape(value) +
                ";domain="+ window.location.hostname +
                ";path=/"+
                ";expires="+expireDate.toUTCString();
        }
    
        function deleteCookie(name) {
            setCookie({name: name, value: "", seconds: 1});
        }
    

    Usage:

    setCookie({name: "reminder", value: "buyCoffee", minutes: 20});
    deleteCookie("reminder");
    
    0 讨论(0)
  • 2020-11-22 02:16

    I had trouble deleting a cookie made via JavaScript and after I added the host it worked (scroll the code below to the right to see the location.host). After clearing the cookies on a domain try the following to see the results:

    if (document.cookie.length==0)
    {
     document.cookie = 'name=example; expires='+new Date((new Date()).valueOf()+1000*60*60*24*15)+'; path=/; domain='+location.host;
    
     if (document.cookie.length==0) {alert('Cookies disabled');}
     else
     {
      document.cookie = 'name=example; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain='+location.host;
    
      if (document.cookie.length==0) {alert('Created AND deleted cookie successfully.');}
      else {alert('document.cookies.length = '+document.cookies.length);}
     }
    }
    
    0 讨论(0)
  • 2020-11-22 02:19

    Here is an implementation of a delete cookie function with unicode support from Mozilla:

    function removeItem(sKey, sPath, sDomain) {
        document.cookie = encodeURIComponent(sKey) + 
                      "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + 
                      (sDomain ? "; domain=" + sDomain : "") + 
                      (sPath ? "; path=" + sPath : "");
    }
    
    removeItem("cookieName");
    

    If you use AngularJs, try $cookies.remove (underneath it uses a similar approach):

    $cookies.remove('cookieName');
    
    0 讨论(0)
  • 2020-11-22 02:22

    You can do this by setting the date of expiry to yesterday.

    Setting it to "-1" doesn't work. That marks a cookie as a Sessioncookie.

    0 讨论(0)
  • 2020-11-22 02:29

    Try this:

    function delete_cookie( name, path, domain ) {
      if( get_cookie( name ) ) {
        document.cookie = name + "=" +
          ((path) ? ";path="+path:"")+
          ((domain)?";domain="+domain:"") +
          ";expires=Thu, 01 Jan 1970 00:00:01 GMT";
      }
    }
    

    You can define get_cookie() like this:

    function get_cookie(name){
        return document.cookie.split(';').some(c => {
            return c.trim().startsWith(name + '=');
        });
    }
    
    0 讨论(0)
提交回复
热议问题