Clearing all cookies with JavaScript

后端 未结 18 1037
别那么骄傲
别那么骄傲 2020-11-22 05:55

How do you delete all the cookies for the current domain using JavaScript?

18条回答
  •  死守一世寂寞
    2020-11-22 06:17

    Jquery:

    var cookies = $.cookie();
    for(var cookie in cookies) {
    $.removeCookie(cookie);
    }
    

    vanilla JS

    function clearListCookies()
    {   
     var cookies = document.cookie.split(";");
     for (var i = 0; i < cookies.length; i++)
      {   
        var spcook =  cookies[i].split("=");
        deleteCookie(spcook[0]);
      }
      function deleteCookie(cookiename)
       {
        var d = new Date();
        d.setDate(d.getDate() - 1);
        var expires = ";expires="+d;
        var name=cookiename;
        //alert(name);
        var value="";
        document.cookie = name + "=" + value + expires + "; path=/acc/html";                    
    }
    window.location = ""; // TO REFRESH THE PAGE
    }
    

提交回复
热议问题