views:

2878

answers:

5

How to dynamically, via javascript, delete a session cookie, without manually restarting the browser?

I read somewhere that session cookie is retained in browser memory and will be removed when the browser is closed.

// sessionFooCookie is session cookie
// this code does not delete the cookie while the browser is still on
jQuery.cookie('sessionFooCookie', null);

Thanks.

More Info: The code snippet above is a javascript code snippet, using jQuery and its jQuery.cookie plugin.

+5  A: 

A session cookie is just a normal cookie without an expiration date. Those are handled by the browser to be valid until the window is closed or program is quit.

But if the cookie is a httpOnly cookie (a cookie with the httpOnly parameter set), you cannot read, change or delete it from outside of the HTTP.

Gumbo
+1  A: 

There are known issues with IE and Opera not removing session cookies when setting the expire date to the past (which is what the jQuery cookie plugin does)

This works fine in Safari and Mozilla/FireFox.

Philippe Leybaert
A: 

Deleting a jQuery cookie:

$(function() {
    var COOKIE_NAME = 'test_cookie';
    var options = { path: '/', expires: 10 };
    $.cookie(COOKIE_NAME, 'test', options); // sets the cookie
    console.log( $.cookie( COOKIE_NAME)); // check the value // returns test
    $.cookie(COOKIE_NAME, null, options);   // deletes the cookie
    console.log( $.cookie( COOKIE_NAME)); // check the value // returns null
});
Elzo Valugi
+1  A: 

This needs to be done on the server-side, where the cookie was issued.

Josh Stodola
A: 

i cant understand your code is it ASP, JS, or PHP

kindly wrote me [email protected]

-1 - this is not an answer
Stephen C