Link to home
Start Free TrialLog in
Avatar of sharingsunshine
sharingsunshineFlag for United States of America

asked on

How Do I Delete A jQuery Cookie Upon Order Submission or Browser Close?

this is a follow on to the question at
https://www.experts-exchange.com/questions/29138630/JQuery-If-Test-Not-Working.html?headerLink=workspace_open_questions

Open in new window


I need the cookie to be deleted when they close the browser or submit the order.   This is the code I have to delete the cookie but it isn't going away for either reason.

jQuery('#cook').click(function(e) {
		e.preventDefault();
		document.cookie = "wantmembership=1; path=/";
document.cookie = 'wantmembership=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';
          jQuery(this).css('background-color','green');
              jQuery(this).text("I Now Have A Membership!");
			});

Open in new window

Avatar of leakim971
leakim971
Flag of Guadeloupe image

why do you need to remove that cookie if you set it ?
Hi sharingsunshine,

To delete a cookie with JQuery, set the value to null:

$.cookie("name", null, { path: '/' });

Open in new window

Avatar of sharingsunshine

ASKER

because it puts a message in the order comments box and if they place another order the same message will be shown again and cause extra work thinking the 2nd message is an initial request.  When in fact, it has already been taken care of.
you can clear this cookie using server code just after you validate the order submission
and initialy a cookie should go away if you don't set the expiration date, so don't set it and you solve your problem

1 - clear cookie with server on order submission
2 - don't set expiration date
Nicolas, when I use your code it inactivates my jQuery button's color and text change.

It does create the cookie but order submission doesn't delete the cookie.
I have tried it with and without an expiration date and it doesn't go away.
and it doesn't go away.

after closing your browser ?
And with this function ?
$.removeCookie('cookie', { path: '/' });

Open in new window

You don't need to delete a session cookie. If you make the expire time 0 the cookie will expire when you close the browser.
I have tried it with and without an expiration date and it doesn't go away.
You should define it like this
document.cookie = "wantmembership=1; path=/;expires=0";

Open in new window


Note that this works if you close the browser - not just the tab - you have to exit the browser instance that was active when the cookie was created.
I did closed the browser. I have closed the tab and then closed the browser.  I have even completed the order and pressed the submit button and in all 3 instances the message persists.

If I do delete the cookie manually the message does go away.
Follow this process to check yoru cookie especially the expiration date :
https://developers.google.com/web/tools/chrome-devtools/storage/cookies
using Julian's code this is what is showing

https://gyazo.com/1ddac87e7089585dbd80eda2afd16ca7

Open in new window

I did closed the browser. I have closed the tab and then closed the browser.  I have even completed the order and pressed the submit button and in all 3 instances the message persists.
Then the cookie was NOT created with expire=0
I believe, after closing the page/browser, you open the page creating the cookie and see it everytime...
Julian,  I have copied your code but when I set it to expires = 0 it doesn't go away when the browser is closed.  So you are correct it isn't being created with expires = 0 so can you help me figure out why?

Nicolas, I don't know how to use your function to only be enacted when the browser is closed.
I am not clear on your last post - are you saying you have changed this (line 4 of your original post)
document.cookie = 'wantmembership=;expires=Thu, 01 Jan 1970 00:00:01 GMT;';

Open in new window

To this
document.cookie = 'wantmembership=;expires=0;';

Open in new window

Sorry for the delay, the message wasn't working anymore.  So, yes I did change line 4 to your suggestion.

It seems when I use
document.cookie = 'wantmembership=;expires=0;';

Open in new window

that it stops writing.  If I use this
document.cookie = "wantmembership=1; expires=0; path=/";

Open in new window

the message gets written but even closing the browser completely the message still appears once a new order is started.
I am confused - are you saying the problem is fixed or you still have a problem?
I still have a problem.  If I follow the first line of code
document.cookie = 'wantmembership=;expires=0;';

Open in new window

the message "I Want Membership" never gets written.

If I use
document.cookie = "wantmembership=1; expires=0; path=/";

Open in new window

the message "I Want Membership" gets written but the cookie and message doesn't go away when the browser closes.
ASKER CERTIFIED SOLUTION
Avatar of Julian Hansen
Julian Hansen
Flag of South Africa image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
I checked my code and it was exactly like yours.  I closed the tab and then exited the browser and then brought the browser back but it still showed the cookie on your code.

I have spent too much time of yours and mine so I appreciate all you have done for me.
thanks for your efforts
Did you check your Task Manager to make sure that all instances of the browser were closed. This is something specific to your setup because the code as you have it is correct.
No, I didn't but I will.  I know it is specific to me that is why I didn't want to waste your time anymore.  Thanks for all the help on this question and all of them in the past too.
You are most welcome