4

HTML links:

<a href="javascript:del_cookie(name);">Logout</a>
<a href="javascript:delete_cookie(rememberKeepMeLoggedIn);">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"?
Does the syntax below look good?

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>

1 Answer 1

3

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 .

Sign up to request clarification or add additional context in comments.

7 Comments

useless string concatenation is useless. Also what if the local machine time is set to 1965 ?
@Raynos: set your machine time to 1965 and try it yourself. On Windows 7, I can't set the system clock earlier than January 1st, 1980.
@MattBall I have to learn how cookies work >_>. I assume your answer will work though. So much code would break if you set your system clock to 1965 anyway.
No luck so far guys... any other suggestions?
@detonate: what's the problem? Did you see the demo? jsfiddle.net/mattball/mJLZj
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.