可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Is my function of creating a cookie correct? How do I delete the cookie at the beginning of my program? is there a simple coding?
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); }
回答1:
Try this:
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"; } }
Or:
function delete_cookie( name ) { document.cookie = name + '=; expires=Thu, 01 Jan 1970 00:00:01 GMT;'; }
回答2:
Here a good link on Quirksmode.
function setCookie(name,value,days) { var expires = ""; if (days) { var date = new Date(); date.setTime(date.getTime() + (days*24*60*60*1000)); expires = "; expires=" + date.toUTCString(); } document.cookie = name + "=" + (value || "") + expires + "; path=/"; } function getCookie(name) { var nameEQ = name + "="; var ca = document.cookie.split(';'); for(var i=0;i
回答3:
would this work?
function eraseCookie(name) { document.cookie = name + '=; Max-Age=0' }
I know Max-Age
causes the cookie to be a session cookie in IE when creating the cookie. Not sure how it works when deleting cookies.
回答4:
You can do this by setting the date of expiry to yesterday.
Setting it to "-1" doesn't work. That marks a cookie as a Sessioncookie.
回答5:
Here is an implementation of a delete cookie function with unicode support from Mozilla:
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");
If you use AngularJs, try $cookies.remove (underneath it uses a similar approach):
$cookies.remove('cookieName');
回答6:
To delete a cookie I set it again with an empty value and expiring in 1 second. In details, I always use one of the following flavours (I tend to prefer the second one):
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); }
Usage:
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}); }
Usage:
setCookie({name: "reminder", value: "buyCoffee", minutes: 20}); deleteCookie("reminder");
回答7:
I had trouble deleting a cookie made via JavaScript and after I added the host it worked. After clearing the cookies on a domain try the following to see the results:
if (document.cookie.length==0) { document.cookie = 'name=example; expires='+new Date((new Date()).valueOf()+1000*60*60*24*15)+'; path=/; domain='+location.host; if (document.cookie.length==0) {alert('Cookies disabled');} else { document.cookie = 'name=example; expires=Thu, 01 Jan 1970 00:00:00 GMT; path=/; domain='+location.host; if (document.cookie.length==0) {alert('Created AND deleted cookie successfully.');} else {alert('document.cookies.length = '+document.cookies.length);} } }