How to Delete Session Cookie?

前端 未结 6 1841
时光取名叫无心
时光取名叫无心 2020-11-30 03:22

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

I read somewhere that session cookie is retained in browser mem

相关标签:
6条回答
  • 2020-11-30 04:04

    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
    });
    
    0 讨论(0)
  • 2020-11-30 04:12

    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

    0 讨论(0)
  • 2020-11-30 04:14

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

    0 讨论(0)
  • 2020-11-30 04:15

    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/

    0 讨论(0)
  • 2020-11-30 04:19

    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).

    0 讨论(0)
  • 2020-11-30 04:24

    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.

    0 讨论(0)
提交回复
热议问题