File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
The moose likes HTML, CSS and JavaScript and the fly likes window.onunload not called on closing the browser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of OCM Java EE 6 Enterprise Architect Exam Guide this week in the OCMJEA forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Bookmark "window.onunload not called on closing the browser" Watch "window.onunload not called on closing the browser" New topic
Author

window.onunload not called on closing the browser

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I am using Google Chrome.

Following code is in my Jsp


On closing the window, this code is not called up (alert is not executed)
While i did the same check using firefox with little change in code as "window.screenX" and "window.screenY", the code is called up only when i close the tab , and not the whole browser
What is the problem?
(I have also checked similar topics in this forum, but, couldn't find the solution)


kunal
Rob Crowther
author
Ranch Hand

Joined: Nov 06, 2012
Posts: 295
    
    5

There's a long discussion of the issues with unload in the blog post WebKit Page Cache II – The unload Event, for more modern browsers try pageshow and pagehide.


Read my books: Hello! HTML5 & CSS3 | HTML5 in Action | Read my blog
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15376
    
    6
When the page unloads you have no control over it. You can not redirect to another page when it is exiting. Session end event on the server should end the session.

Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
There is a requirement in my application, where only one user with a particular username can log in at a time. Also, if he is logged in, the status in db changes to LOGGED IN. When he logs out, status chnages to LOGGED OUT.
Suppose, i set the time out to 15mins, or session inactiave to 5mins. The user accidently closes the browser. Then, status remains LOGGED IN, unless and until, that time period of 5mins/15mins is over.
That's the reason why, i need to invalidate the session as soon as browser closes (or PC shuts down)
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61105
    
  66

Requirements that cannot be met need to be changed.


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Kunal Lakhani
Ranch Hand

Joined: Jun 05, 2010
Posts: 622
I wish i could. But, all depends on my seniors. I was just said to implement this
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61105
    
  66

Well, as there is no way for this to be accomplished, you have a problem. You can either:
  • Let whoever is setting the requirement know that the session timeout is the only way to know if a browser has been adandoned
  • Or, continue to waste time trying to find a way to do this when it cannot be accomplished.

  • Bottom line: you can not get any reliable indication that a browser has been abandoned, or that a PC has been shut down, or has crashed. In these cases, you need to rely upon the session timeout.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    Thanks Bear,Eric Pascarello,Rob Crowther for your replies
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    Hello
    I have got another situation, which is causing problem.
    My servlet class has following lines in the beginning :

    Suppose the user closes the browser, and session still is active (some mins remaining to make it inactive)
    If some user, without login in, types any url specified in the web.xml, which calls any servlet. Then, that will change the db details, since it will satisfy the condition stated above in if block
    That's the reason why i want to invalidate the session on closing of browser
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61105
        
      66

    Again, it does not matter why you want to do it. It is the responsibility of the user of the application to log out of the application, and for you to set the session timeout to an appropriate value.

    P.S. Why the check for isNew()? It's superfluous.
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    P.S. Why the check for isNew()? It's superfluous.


    Will username == null alone do?
    Jayesh A Lalwani
    Bartender

    Joined: Jan 17, 2008
    Posts: 2347
        
      28

    Hmm if user closes the browser, and another user logs in, the second user should get a different http session
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61105
        
      66

    Kunal Lakhani wrote:
    P.S. Why the check for isNew()? It's superfluous.


    Will username == null alone do?


    Think about it. Will there ever be a new session with a username in it?
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    Think about it. Will there ever be a new session with a username in it?


    No. Not at all.
    Bear Bibeault
    Author and ninkuma
    Marshal

    Joined: Jan 10, 2002
    Posts: 61105
        
      66

    Right. So why check for it?
    Kunal Lakhani
    Ranch Hand

    Joined: Jun 05, 2010
    Posts: 622
    Right

    Thanks Bear Bibeault, Jayesh A Lalwani, Eric Pascarello, Rob Crowther
     
    I agree. Here's the link: http://aspose.com/file-tools
     
    subject: window.onunload not called on closing the browser