如何破坏cookie

时间:2014-01-11 15:20:13

标签: jquery cookies

我正在创建一个cookie,阅读它然后我想要销毁它。 我该怎么做?

function createCookie(name,value,days) { //create a cookie to store authorisation details on the clients machine
if (days) {
var date = new Date();
date.setTime(date.getTime()+(days*24*60*60*1000));
var expires = "; expires="+date.toGMTString();

}
else var expires = "";
document.cookie = name+"="+value+expires+"; path=/";
 }


 function readCookie(name) { //read the cookie on the clients machine
 var nameEQ = name + "=";
var ca = document.cookie.split(';');
for(var i=0;i < ca.length;i++) {
 var c = ca[i];
 while (c.charAt(0)==' ') c = c.substring(1,c.length);
  }
 return null;
 }

我想在用户按下注销按钮

时使此cookie过期

1 个答案:

答案 0 :(得分:0)

您可以设置过去的过期日期。

function deleteCookie(name) {
    document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:00 GMT;';
}

或类似的东西