How to delete a cookie?

后端 未结 8 1697
说谎
说谎 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: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');
    

提交回复
热议问题