Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to delete all cookies with jquery [duplicate]

Tags:

jquery

cookies

Possible Duplicate:
Clearing all cookies with javascript

I would like to have a checkbox assigned to activate and wipe out all cookies previously stored in my forms in one go. How would I do that with jquery cookie plugin? I can't seem to find examples in Klaus site and here.

Any hint would be very much appreciated.

Thanks

like image 916
swan Avatar asked Feb 12 '10 17:02

swan


2 Answers

The accepted answer in this question should accomplish what you're after:

var cookies = document.cookie.split(";");
for(var i=0; i < cookies.length; i++) {
    var equals = cookies[i].indexOf("=");
    var name = equals > -1 ? cookies[i].substr(0, equals) : cookies[i];
    document.cookie = name + "=;expires=Thu, 01 Jan 1970 00:00:00 GMT";
}

(code is expanded for clarity from the linked answer, no need to reinvent the wheel here)

There's no need for a plugin in all cases, sometimes a simple JavaScript snippet will do...jQuery really doesn't help at all here

like image 135
Nick Craver Avatar answered Nov 15 '22 01:11

Nick Craver


You do not need to use jquery for that, only pure javascript:

function setCookie(name, value, seconds) {

    if (typeof(seconds) != 'undefined') {
        var date = new Date();
        date.setTime(date.getTime() + (seconds*1000));
        var expires = "; expires=" + date.toGMTString();
    }
    else {
        var expires = "";
    }

    document.cookie = name+"="+value+expires+"; path=/";
}

And call with setCookie( cookieName, null, -1);

like image 21
Artem Barger Avatar answered Nov 15 '22 00:11

Artem Barger