The following method shows how to delete a cookie key-value pair with JavaScript on the client browser.
//The method of deleting a cookie using JavaScript, which passes in the name of the cookie to be deletedfunctionRemovecookie (cookiename) {varcookies = Document.cookie.split (";");//divides all cookie key pairs into arrays by semicolons //loops through all cookie key-value pairs for(vari = 0; i < cookies.length; i++) { //some cookie key value pairs in the front will somehow produce a space, the space is removed if(Cookies[i].indexof ("") = = 0) {Cookies[i]= cookies[i].substring (1); } //Compare the name of each cookie to find the cookie key value pair to delete if(Cookies[i].indexof (cookiename) = = 0) { varExp =NewDate ();//get client local current system timeExp.settime (Exp.gettime ()-60 * 1000);//set Exp to client local time 1 minutes ago, when the EXP is assigned to the cookie as the expiration time, it means that the cookie has expired, and the browser deletes it immediately.Document.cookie= Cookies[i] + "; expires=" + exp.toutcstring ();//set the expiration time of the cookie to be deleted, i.e. add a expires key pair after the key value pair of the cookie and assign the above exp to expires as the value (note that the value of expires must be UTC or GMT time, not local time), Then the browser will delete the cookie immediately . //Note that the use of document.cookie is ingenious, when assigning a value to the information of a single cookie, but the time to get the value of Document.cookie is to return all the cookie information Break;//the cookie to be deleted has been removed from the client and is out of loop } }}
Delete a cookie key value pair with JavaScript on the client