67

How to dynamically, via jQuey, 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);

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

A-Sharabiani
  • 17,750
  • 17
  • 113
  • 128
Nordin
  • 3,087
  • 5
  • 28
  • 35

7 Answers7

108

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 HTTP (meaning it must be changed on the server).

Greg Gum
  • 33,478
  • 39
  • 162
  • 233
Gumbo
  • 643,351
  • 109
  • 780
  • 844
  • 16
    I must say, that I was beating by head against the monitor about an hour with question _"Why I'm not able to delete session cookie from the javascript?"_, until I haven't found the short answer: it was `httpOnly`... Thanks, very useful. – BlitZ Jul 08 '16 at 03:54
  • 1
    FYI: with _the HTTP_ in @Gumbo's last sentence, he actually means _server side_. – ThomasDC Oct 23 '17 at 07:49
38

Be sure to supply the exact same path as when you set it, i.e.

Setting:

$.cookie('foo','bar', {path: '/'});

Removing:

$.cookie('foo', null, {path: '/'});

Note that

$.cookie('foo', null); 

will NOT work, since it is actually not the same cookie.

Hope that helps. The same goes for the other options in the hash

Mads Buus
  • 488
  • 5
  • 5
6

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
  • 168,566
  • 31
  • 210
  • 223
3

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

Josh Stodola
  • 81,538
  • 47
  • 180
  • 227
2

you can do this by setting the date of expiry to yesterday.

My new set of posts about cookies in JavaScript could help you.

http://www.markusnordhaus.de/2012/01/20/using-cookies-in-javascript-part-1/

Peter O.
  • 32,158
  • 14
  • 82
  • 96
Markus Nordhaus
  • 223
  • 2
  • 2
-1

If it is a session cookie + hostOnly cookie (like default tapestry session's cookies) To delete from Javascript you also must not specify domain when editing/expire cookie, and not specify expire date and set value to null.

function clearHOnlySessionCookie(name,  path){
    var domain = domain || document.domain;
    var path = path || "/";
    document.cookie = name + "=null; path=" + path+";";
};
Alberto Perez
  • 1,019
  • 15
  • 17
-3

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
  • 27,240
  • 15
  • 95
  • 114