JavaScript the correct way to delete cookies (reproduced)

Source: Internet
Author: User

The original always thought that as long as the Document.cookie object set JavaScript can simply set and delete cookie value in the browser side, many articles on the web is also taught, but recently found that the simple set of JavaScript Document.cookie value can not be completely deleted or change the cookie. Accidentally found an article gives the correct method, which is posted here.

Use JavaScript to clean up cookies first find the value corresponding to the name of the cookie, and then set it to expire:
The following methods are found for cookies:

function GetCookie2 (name) {    var arr = document.cookie.match (New RegExp ("(^|)" +name+ "= ([^;] *)(;|$)"));      if (arr! = null)         return unescape (arr[2]); return null;}

When found, set to expire, remember to set domain and path, only these two parameters are exactly the same as the parameters you want to delete to delete it.

function Resetnfluent () {alert ("before=>" +document.cookie), var exp  = new Date (); Exp.settime (Exp.gettime ()-1) var cval=getcookie2 (' name '), var Lanobj=document.getelementbyid (' lanoption '), var lansel=lanobj.value;alert (LanSel if (lansel== ' en ') {alert (' let\ ' s reset nfluent '); alert (' cval=> ' +cval); if (cval!=null) {document.cookie= "name=" + cval+ ";d omain=.example.com;expires=" +exp.togmtstring () + ";p ath=/";} Else{document.cookie= "Name=;d omain=.example.com;expires=" +exp.togmtstring () + ";p ath=/";}} Else{alert (' don\ ' t need reset nfluent ');} Alert ("after=>" +document.cookie);    

As for how to view cookies in detail, such as path and domain, in Firefox can be viewed according to the following steps:
Tools->option->privacy->show Cookies

Original address

JavaScript the correct way to delete cookies (reproduced)

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.