如何删除一个cookie?
我的创buildcookie的function是否正确? 如何在我的程序开始时删除cookie? 有没有简单的编码?
function createCookie(name,value,days)
function setCookie(c_name,value,1) { document.cookie = c_name + "=" +escape(value); } setCookie('cookie_name',mac); function eraseCookie(c_name) { createCookie(cookie_name,"",-1); }
尝试这个:
function delete_cookie( name, path, domain ) { if( get_cookie( name ) ) { document.cookie = name + "=" + ((path) ? ";path="+path:"")+ ((domain)?";domain="+domain:"") + ";expires=Thu, 01 Jan 1970 00:00:01 GMT"; } }
要么:
function delete_cookie( name ) { document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; }
这里是Quirksmode的一个很好的链接。
function createCookie(name,value,days) { 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) { 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); if (c.indexOf(nameEQ) == 0) return c.substring(nameEQ.length,c.length); } return null; } function eraseCookie(name) { createCookie(name,"",-1); }
这会工作吗?
function eraseCookie(name) { document.cookie = name + '=; Max-Age=0' }
我知道Max-Age
会导致cookie在创buildcookie时成为IE中的会话cookie。 不知道如何删除cookies。
你可以通过设置到期date为昨天。
将其设置为“-1”不起作用。 这标志着一个cookie作为Sessioncookie。
以下是Mozilla提供的unicode支持的删除cookiefunction的实现:
function removeItem(sKey, sPath, sDomain) { document.cookie = encodeURIComponent(sKey) + "=; expires=Thu, 01 Jan 1970 00:00:00 GMT" + (sDomain ? "; domain=" + sDomain : "") + (sPath ? "; path=" + sPath : ""); } removeItem("cookieName");
如果你使用AngularJs,试试$ cookies.remove (在它下面使用类似的方法 ):
$cookies.remove('cookieName');
要删除一个cookie,我用一个空值重新设置,并在1秒内过期。 在细节上,我总是使用以下口味之一(我倾向于第二个):
1。
function setCookie(key, value, expireDays, expireHours, expireMinutes, expireSeconds) { var expireDate = new Date(); if (expireDays) { expireDate.setDate(expireDate.getDate() + expireDays); } if (expireHours) { expireDate.setHours(expireDate.getHours() + expireHours); } if (expireMinutes) { expireDate.setMinutes(expireDate.getMinutes() + expireMinutes); } if (expireSeconds) { expireDate.setSeconds(expireDate.getSeconds() + expireSeconds); } document.cookie = key +"="+ escape(value) + ";domain="+ window.location.hostname + ";path=/"+ ";expires="+expireDate.toUTCString(); } function deleteCookie(name) { setCookie(name, "", null , null , null, 1); }
用法:
setCookie("reminder", "buyCoffee", null, null, 20); deleteCookie("reminder");
2
function setCookie(params) { var name = params.name, value = params.value, expireDays = params.days, expireHours = params.hours, expireMinutes = params.minutes, expireSeconds = params.seconds; var expireDate = new Date(); if (expireDays) { expireDate.setDate(expireDate.getDate() + expireDays); } if (expireHours) { expireDate.setHours(expireDate.getHours() + expireHours); } if (expireMinutes) { expireDate.setMinutes(expireDate.getMinutes() + expireMinutes); } if (expireSeconds) { expireDate.setSeconds(expireDate.getSeconds() + expireSeconds); } document.cookie = name +"="+ escape(value) + ";domain="+ window.location.hostname + ";path=/"+ ";expires="+expireDate.toUTCString(); } function deleteCookie(name) { setCookie({name: name, value: "", seconds: 1}); }
用法:
setCookie({name: "reminder", value: "buyCoffee", minutes: 20}); deleteCookie("reminder");