2014-01-11 204 views
0

我正在創建一個cookie,讀取它,然後我想銷燬它。 我該怎麼做?如何銷燬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; 
} 

我希望當用戶按下注銷按鈕

回答

0

您可以在過去的到期日期過期這個cookie。

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

什麼呈三角

+0

而據我知道這是唯一的方法。你只需要在現在設置過期日期。 – TrN

+0

是的,但會過期,我希望它被刪除時,用戶按下注銷按鈕,刷新它刪除它但我沒有寫任何代碼,有沒有什麼辦法,我可以刷新頁面,當退出時按下 – user3051827

+1

@ user3051827您不能強制客戶端瀏覽器物理刪除cookie。您只能將其設置爲過期並將垃圾存儲到其值中。 – Dukecz