How to delete a cookie?

后端 未结 8 1679
说谎
说谎 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: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 + '=');
        });
    }
    

提交回复
热议问题