Recently in a project, JS Delete cookie is always unsuccessful, and finally found that there is no specified cookie domain
JS Delete Cookie is to change the expiration time of the cookie, the expiration time of the cookie is set to the past, the following items must be specified
1.cookie Name
2.cookie value
3.cookie expiry time
4.cookie Domain
5.cookie Path
The above are indispensable, must have the ability to modify the cookie
The specific code is as follows:
1 /** Set Cookies2 * @param {cookie name}3 * @param {cookie value}4 * @param {set cookie domain}5 * @param {Expiration time (days)} [VarName]6 */7 functionSetcookie (name, value, domain, day) {8 varDate =NewDate ();9 TenDate.settime (Date.gettime () +1000*60*60*24*Day ); OneDocument.cookie = name+ ' = ' +value+ '; expires= ' +date+ ';d omain= ' +domain+ ';p ath=/'; A } - - /** the * Get Cookies - * @return {[type]} [description] - */ - functionGetCookie (name) { + vardata = Document.cookie.split ('; ')), -param = {}; + for(vari = 0; i < data.length; i++) { AParam[data[i].split (' = ') [0].replace (/\s/, ')] = data[i].split (' = ') [1]; at } - returnParam[name]; - } - - /** - * Delete Cookies in * Name: Delete cookie name - * domain: Domains in which you are located to */ + functionDelcookie (name, domain) { -Document.cookie = name+ ' = ' +getcookie (name) + '; expires= ' + (NewDate (1)) + ';d omain= ' +domain+ ';p ath=/'; the}
For example, now I want to delete _uij This cookie, I view the following cookie's domain as the. renren.com from the Chrome developer tools, application Panel cookie option (note that there is a. in front of Renren)
Delcookie (' _uij ', '. renren.com ');
You can delete it.
JS Delete Cookie is unsuccessful