Delete or reset a browser cookie via Javascript
HTML links:
<a href="javascript:del_cookie(name);">Logout</a>
<a href="javascript:delete_cookie(rememberKeepMeLog开发者_JS百科gedIn);">Logout</a>
<a href="javascript:eraseCookie(name);">Logout</a>
<a href="#" onclick="del_cookie(name);">Logout</a>
<a href="#" onclick="delete_cookie(rememberKeepMeLoggedIn);">Logout</a>
<a href="#" onclick="eraseCookie(name);">Logout</a>
Javascript:
function delete_cookie(rememberKeepMeLoggedIn) {
var cookie_date = new Date ( ); cookie_date.setTime ( cookie_date.getTime() - 1 ); document.cookie = rememberKeepMeLoggedIn += "=; expires=" + cookie_date.toGMTString(); }function del_cookie(name) { document.cookie = 'acceptsCookies=; expires=Thu, 01 Jan 1970 00:00:00 GMT;';
window.location = "http://www.smugmug.com/logout.mg?goTo=#" }function eraseCookie(name) { var cookies = document.cookie.split(";");
for (var i = 0; i < cookies.length; i++) eraseCookies(cookies[i].split("=")[0]); }
How can I delete or reset a cookie with the name "UP-759283
"?
Here's my javascript function:
function del_cookie() {
document.cookie = UP-759283 +'=; expires=Thu, 01-Jan-70 00:00:01 GMT;'; }
The HTML URL callout
<a href="javascript:del_cookie(); document.location.reload( );">Logout</a>
Does the syntax below look good?
No; just run your code through JSLint and you'll see. Change
document.cookie = UP-759283 +'=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
to
document.cookie = 'UP-759283=; expires=Thu, 01-Jan-70 00:00:01 GMT;';
As per the MDC document.cookie
docs, cookies are deleted by setting the expiration time to zero:
document.cookie = 'UP-759283=; expires=Thu, 01 Jan 1970 00:00:00 GMT;';
Other reference: cookies @ quirksmode .
精彩评论