5

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

Community
  • 1
  • 1
swan
  • 2,509
  • 3
  • 24
  • 40
  • this question is not a duplicate as the other is specific to plain javascript and this one is specific to jQuery. – VUELA Aug 21 '13 at 18:50
  • Agreed. The whole purpose of jquery is to avoid writing boilerplate javascript. The correct answer to this is: `for (var it in $.cookie()) $.removeCookie(it);` But I can't post it because this has been flagged as a dup. (Requires jquery-cookie.) – Scott Smith Feb 11 '16 at 17:44
  • Also, the accepted answer does not account for the path being set on the cookie. Another reason to use a library instead of writing raw js. – Scott Smith Feb 11 '16 at 17:45

2 Answers2

6

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

Community
  • 1
  • 1
Nick Craver
  • 623,446
  • 136
  • 1,297
  • 1,155
  • Been there before, but was too eager to stay in jquery :) So I guess the answer is no easy way to wipe out cookies with jquery? Gotta grab the closest hand I can get then. Thanks – swan Feb 12 '10 at 17:46
  • jQuery is just a bunch of javascript, don't think of them like you're delegating out to something different...it's all the same stuff. Wrapping some things in a plugin isn't needed a fair portion of the time, it's usually just equivalent to changing the namespace for what some plugins do. – Nick Craver Feb 12 '10 at 17:51
  • 1
    You didn't answer the actual question related to jquery, but the link did answer my need for now. Thanks – swan Feb 13 '10 at 04:53
  • 2
    This answer does not say how to do it in JQuery – KevinDeus Jun 17 '11 at 01:06
  • 3
    @KevinDeus - jQuery is just JavaScript, and there are many cases where you don't need jQuery, just plain JavaScript will do. – Nick Craver Jun 17 '11 at 02:03
  • @VUELA you can wrap this in a function, nothing prevents you from doing so...jQuery doesn't have anything to help here. – Nick Craver Aug 21 '13 at 19:52
  • @NickCraver thanks! i got it, i see your point. – VUELA Aug 21 '13 at 20:54
  • How can this be modified to work for all paths / domains? – VUELA Aug 21 '13 at 20:55
  • just something i ran into... you can only remove cookies matching the current domain you're on. for instance if you're on www.google.com you cant remove www.bing.com cookies – Craig Wayne Oct 10 '14 at 09:45
  • @VUELA it can't - that would be a tremendous be a security hole. Stack Overflow shouldn't be able to alter your bank website's cookies, for example. – Nick Craver Oct 10 '14 at 11:42
  • @CraigWayne yes - this is absolutely by design. Again, it would be a huge security hole to allow cross-domain editing of cookies, localStorage, etc. – Nick Craver Oct 10 '14 at 11:43
2

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

Artem Barger
  • 40,769
  • 9
  • 59
  • 81
  • 3
    but he want to do this using jQuery. – Rakesh Juyal Feb 12 '10 at 17:44
  • 1
    Yes, I love to see jquery version if any please. But I am always listening to any possible solution. Thanks – swan Feb 12 '10 at 17:56
  • 7
    Listen you two, jQuery **is** Javascript. There is absolutely no reason to want it one way or the other, because they are the same. jQuery should not be used to do this. – Josh Stodola Feb 12 '10 at 18:49