aspose file tools*
The moose likes JSP and the fly likes Handle window close event Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


Win a copy of The Java EE 7 Tutorial Volume 1 or Volume 2 this week in the Java EE forum
or jQuery UI in Action in the JavaScript forum!
JavaRanch » Java Forums » Java » JSP
Bookmark "Handle window close event" Watch "Handle window close event" New topic
Author

Handle window close event

Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
I want to call a servlet when a window is closed.I used onunload(),but that is triggered even at the times when i refresh the page or click a link on the page.
But, i want that my function should be triggered only when i close the window.
Can anyone tell me how can i do that.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

There is no way to detect this.

What is it that you feel that you need to do on window close?


[Asking smart questions] [Bear's FrontMan] [About Bear] [Books by Bear]
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Actually, we have some files on server side that needs to be deleted when the client closes the browser.When client is refreshing the window or if he is clicking the link, it means he still wants to view the window.So, in that case we can not delete the files.We have to delete the files, only when we are sure user is done with viewing our window and now he is closing the window.

Please tell me the solution if the situation is clear to you.
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

You'll have to rely on the session timeout.
ShaikSalam Bashoeb
Greenhorn

Joined: Dec 10, 2008
Posts: 24
Hi Raj, there is one nice solution for your problem is

use onunload event in body tag and i hope your calling one javascript method. in that method you can check your window got closed or not by using


I hope you got the solution.................
[ December 14, 2008: Message edited by: ShaikSalam Bashoeb ]
Bear Bibeault
Author and ninkuma
Marshal

Joined: Jan 10, 2002
Posts: 61064
    
  66

Originally posted by ShaikSalam Bashoeb:
use onunload event in body tag

That event is fired for all cases where the page is unloaded, including navigation. It makes no distinction on why the page is being unloaded.
Bauke Scholtz
Ranch Hand

Joined: Oct 08, 2006
Posts: 2458
Besides, the onunload (and onbeforeunload) events aren't specified in W3 HTML specification. Although the major webbrowsers supports them, you shouldn't rely that the webbrowser used by the client supports it. For example Opera, which usually follows the W3 specs extremely strictly, doesn't support it.

As Bear said, do it when the session times out. You can use HttpSessionListener or maybe HttpSessionBindingListener for this.
Raj Kumar Bindal
Ranch Hand

Joined: Apr 15, 2006
Posts: 418
Thanks for your suggestions
 
With a little knowledge, a cast iron skillet is non-stick and lasts a lifetime.
 
subject: Handle window close event