4

i need to delete cookie when browser is closed and already put on window.onbeforeunload along with other as following:

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

with setCookie as following:

 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 as below:

    setCookie(name,value,-1);

The problem is whenever i restart browser, it always comes to window.onbeforeunload so that deleteCookie fired. I actually need this to reset my countdown timer whenever user logs in since the cookie never deleted if the counter doesn't end and user sometimes closes window/tab before it ends. So, my idea is either reset the counter when user logs in or simply delete cookie when user logs out. I still can't figure out how to code this, however. Can anyone help me out? Code snippet will be appreciated, though. CMIIW

Doni Andri Cahyono
  • 793
  • 5
  • 16
  • 28
  • 1
    I'm not 100% clear on the question but maybe your want a session instead, take a read of http://stackoverflow.com/questions/5608764/purpose-of-php-sessions-and-cookies-and-their-differences. – PeterJ Dec 14 '12 at 03:44
  • @PeterJ: I'm currently using JSP or JSTL, how can i implement this via session since i need this not to dect first page but to detect after login which redirected to another page. I already attempted to use session.isNew() but it only worked for index.jsp. CMIIW – Doni Andri Cahyono Dec 14 '12 at 03:48
  • I don't know how you're initializing the cookie, but if you don't provide an expiration for it, it will expire when you close the browser. Not sure if that helps... – Ian Dec 14 '12 at 03:48
  • @Ian: I initialize the cookie by using setCookie('Times,value, 1) which 1 refers to 1 day. I made it that way since in my codes, when the counter ends, deleteCookie will be fired. CMIIW – Doni Andri Cahyono Dec 14 '12 at 03:51

2 Answers2

6

Not specifying Expires value on the cookie will cause the cookie to be removed when the browser session ends i.e. when user closes the browser. Like:

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

The made_write_conn cookie made_write_conn does not have an expiration date, making it a session cookie. It will be deleted after the user closes his/her browser. Try doing:

setCookie('Times',value, '');
Sudhir Bastakoti
  • 99,167
  • 15
  • 158
  • 162
0

You can not change the page's location when the browser is being closed.

On the server you should be using session end events to clean up the account.

Community
  • 1
  • 1
epascarello
  • 204,599
  • 20
  • 195
  • 236