How to delete a cookie?

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

提交回复
热议问题