Delete a cookie key value pair with JavaScript on the client

Source: Internet
Author: User

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

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.