Delete cookie when closing browser

I need to delete a cookie when the browser is closed and already placed in window.onbeforeunload along with another one of the following:

window.onbeforeunload = function(){ location.replace("admin.jsp?action=logout"); deleteCookie('Times'); } 

with setCookie as follows:

  function setCookie(name,value,days) { var expires; if (days) { var date = new Date(); date.setTime(date.getTime()+(days*24*60*60*1000)); expires = "; expires="+date.toGMTString(); } else { expires = ""; } document.cookie = name+"="+value+expires+"; path=/"; } 

when it comes to deleteCookie, it contains the following:

  setCookie(name,value,-1); 

The problem is that when I restart the browser, it always comes to window.onbeforeunload, so deleteCookie starts. I really need this before resetting my countdown timer whenever the user logs in, since the cookie is never deleted if the counter does not end, and the user sometimes closes the window / tab before it ends. So my idea is to either reset the counter when the user logs in or just delete the cookie when the user logs out. However, I still cannot figure out how to code this. Can someone help me? However, the code snippet will be appreciated. CMIIW

+4
source share
2 answers

Not specifying the Expires value in the cookie will delete the cookie at the end of the browser session, that is, when the user closes the browser. How:

 Set-Cookie: made_write_conn=1295214458; Path=/; Domain=.foo.com 

The made_write_conn cookie made_write_conn cookie does not have an expiration date, which makes it a session cookie. It will be deleted after the user closes his browser. Try to do:

 setCookie('Times',value, ''); 
+6
source

You cannot change the location of the page when you close the browser.

On the server, you must use session end events to clear the account.

0
source

All Articles