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

3
  • this question is not a duplicate as the other is specific to plain javascript and this one is specific to jQuery. Commented Aug 21, 2013 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.) Commented Feb 11, 2016 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. Commented Feb 11, 2016 at 17:45

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

10 Comments

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
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.
You didn't answer the actual question related to jquery, but the link did answer my need for now. Thanks
This answer does not say how to do it in JQuery
@KevinDeus - jQuery is just JavaScript, and there are many cases where you don't need jQuery, just plain JavaScript will do.
|
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);

3 Comments

but he want to do this using jQuery.
Yes, I love to see jquery version if any please. But I am always listening to any possible solution. Thanks
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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.