2

I'm trying to erase completely a cookie that is set if a particular URL parameter is present.

I've tried:

function delete_cookie (cookie_name) {
    var cookie_date = new Date ();
    cookie_date.setTime(cookie_date.getTime()-1);
    document.cookie = cookie_name += "=; expires=" + cookie_date.toGMTString();
}

delete_cookie('MyCookie');

And it sets the cookie value to null but the cookie itself or at least the name of the cookie stays in the browser so that when the same URL is opened again, it will not update the cookie value from null to XXX nor is a second cookie with the URL value created.

For the new cookie to be setup again I have to right-click on the cookie name (in Chrome) and choose delete. This way the cookie name and its value null are truly erased otherwise it stays with null value.

How can I completely erase the cookie file and not just set it to null with javascript?

** NEW INFO **

I should mention that I also have the following function to grab the cookie from the URL parameter and to keep it from the first page until the final page (where it should be deleted after the cookie value is added to another URL). The second page will turn the link into a session where the URL parameter is no longer visible

Here is the code for it:

function URLparam(name) {
return unescape(
    (RegExp(name + '(?:\\[\\d+\\])?=' + '(.+?)(&|$)').exec(location.search)||[,null])[1]
);
}

$(document).ready(function () {

if (URLparam("MyParameter") !== null && !get_cookie ("MyCookie")) {
    set_cookie ("MyCookie", URLparam("MyParameter"));
}

Any ideas?

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
adrian.m123
  • 85
  • 1
  • 3
  • 11
  • 1
    So what is the *actual* cookie expiration time after you run this code? – zerkms Sep 23 '14 at 23:37
  • Are you sure you don't want to use something like: http://stackoverflow.com/questions/2144386/javascript-delete-cookie – Ian Sep 23 '14 at 23:43
  • @zerkms I bet it's still in the future, which won't delete the cookie. The link I just posted makes it seem like as long as you set the expiration in the past, the cookie will be deleted. Sound right? I feel like this is a duplicate of that link – Ian Sep 23 '14 at 23:43
  • @Ian not only that. Another guess is that it's set for another domain/path. But still looks like a duplicate indeed. – zerkms Sep 23 '14 at 23:44
  • @Ian The expiration date stays as "Session" and doesn't change – adrian.m123 Sep 24 '14 at 01:15
  • @zerkms I have done as advised below but the problem persists. I have added more info to my original question. Any ideas? – adrian.m123 Sep 25 '14 at 09:01

1 Answers1

4

The easiest way to delete the cookie would be like this:

function delete_cookie( name ) {
    document.cookie = name + '=; path=/; expires=Thu, 01 Jan 1970 00:00:01 GMT;';
}

Updated: Had to add the path to get it to work.

Here's a Plunk showing it working. The top buttons in the Plunk use a library to set/view/delete the null cookie. Use the bottom button to delete with the old school method (above) and then use the library show button to verify it is gone.

Michael Oryl
  • 20,856
  • 14
  • 77
  • 117
  • I tried it but it won't delete it. The expiration date stays as "Session". Where can I check that path matches the page I'm running code on and how would the code looks if the path is different? – adrian.m123 Sep 24 '14 at 01:16
  • @adrian.m123 You need to include the path. Sorry about that. Check out the modified answer with the plunk showing it in action. – Michael Oryl Sep 24 '14 at 13:55
  • I have done it and it indeed sets the path to `/`, domain matches too but it will not delete the cookie, it will simply set it to `NULL`. Myabe I should mention that I also have the following function to grab the cookie and keep it from the first page until the final page. The second page will turn the link into a session. – adrian.m123 Sep 25 '14 at 08:42
  • I have added more info to the question – adrian.m123 Sep 25 '14 at 09:00
  • @adrian.m123 I don't really see how your update pertains to the problem. If you have something in your app flow that is re-creating the cookie after it has been deleted, then I probably can't help you. I have showed you how to delete the cookie - even when it is set to null. The Plunk lets you see that for yourself. – Michael Oryl Sep 25 '14 at 15:27
  • I'm sorry. I found the problem. I was setting the cookie with a URparam function. I tried it with a getParameterByName function and now everything works as you said. Thanks! – adrian.m123 Sep 27 '14 at 08:21