Win a copy of Programmer's Guide to Java SE 8 Oracle Certified Associate (OCA) this week in the OCAJP forum!
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

window.onunload not called on closing the browser

 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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)
 
Rob Crowther
author
Ranch Hand
Posts: 297
5
Android Fedora Firefox Browser
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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.
 
Eric Pascarello
author
Rancher
Posts: 15385
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Requirements that cannot be met need to be changed.
 
Kunal Lakhani
Ranch Hand
Posts: 622
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I wish i could. But, all depends on my seniors. I was just said to implement this
 
Bear Bibeault
Author and ninkuma
Marshal
Pie
Posts: 65118
89
IntelliJ IDE Java jQuery Mac Mac OS X
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
    Posts: 622
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Thanks Bear,Eric Pascarello,Rob Crowther for your replies
     
    Kunal Lakhani
    Ranch Hand
    Posts: 622
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 65118
    89
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 622
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    P.S. Why the check for isNew()? It's superfluous.


    Will username == null alone do?
     
    Jayesh A Lalwani
    Rancher
    Posts: 2756
    32
    Eclipse IDE Spring Tomcat Server
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 65118
    89
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Posts: 622
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    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
    Pie
    Posts: 65118
    89
    IntelliJ IDE Java jQuery Mac Mac OS X
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Right. So why check for it?
     
    Kunal Lakhani
    Ranch Hand
    Posts: 622
    • Mark post as helpful
    • send pies
    • Quote
    • Report post to moderator
    Right

    Thanks Bear Bibeault, Jayesh A Lalwani, Eric Pascarello, Rob Crowther
     
    • Post Reply
    • Bookmark Topic Watch Topic
    • New Topic