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 calling a javascript function on closing browser Big Moose Saloon
  Search | Java FAQ | Recent Topics
Register / Login


Win a copy of Practical Unit Testing with TestNG and Mockito this week in the Testing forum!
JavaRanch » Java Forums » Engineering » HTML, CSS and JavaScript
Reply Bookmark "calling a javascript function on closing browser" Watch "calling a javascript function on closing browser" New topic
Author

calling a javascript function on closing browser

B Bhutta
Greenhorn

Joined: Jun 28, 2002
Posts: 14
Hi,
I am trying to call some javascript function, e.g. logoff, when some body click on 'X' on the browser (top right corner). When I tried to use onUnload method, this method is called everytime we changes the page.
I am trying to kill my java session and everything, doing proper logoff, whensomebody closes the browser.
Any help will be appreciated.
Thanks.
Eric Pascarello
author
Rancher

Joined: Nov 08, 2001
Posts: 15003
Your name does not match the naming policy of the forums.
Your name isn't even close to the JavaRanch Naming Policy. Your name should be two words, firstname then lastname with a space in the middle. This naming policy is enforced to promote a professional atmosphere in the forums.
Note: I once used a name like yours and I have since changed it.
You can change your display name here:
http://www.coderanch.com/forums/user/edit
Eric

Now for the question. IE has onBeforeunload and that is is your only option, unless you use seesion cookies and check them, but I do not have time to go into it since I have to goto work.
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
Originally posted by Babar:
Hi,

I am trying to kill my java session and everything, doing proper logoff, whensomebody closes the browser.
Any help will be appreciated.
Thanks.

you just have to invalidate the session. Dont use js for that. When the user closes the browser the session becomes invalid. If u have a log off button then u have to invalidate the session
Bear Bibeault
Author and opinionated walrus
Marshal

Joined: Jan 10, 2002
Posts: 50674

When the user closes the browser the session becomes invalid.

Incorrect. The session has no idea that the browser was closed. It will timeout (after whatever period is specified on the server) if no further activity takes place, but the session will not automatically invalidate upon browser close.
hth,
bear


[Smart Questions] [JSP FAQ] [Books by Bear] [Bear's FrontMan] [About Bear]
Naveen BK
Greenhorn

Joined: Apr 30, 2003
Posts: 1
Originally posted by Babar:
Hi,
I am trying to call some javascript function, e.g. logoff, when some body click on 'X' on the browser (top right corner). When I tried to use onUnload method, this method is called everytime we changes the page.
I am trying to kill my java session and everything, doing proper logoff, whensomebody closes the browser.
Any help will be appreciated.
Thanks.

Hi..one solution for this is that using javascript...in the page have a hidden field control say hdCloseFlag, with initial value "Yes".
If u have some buttons like Save,etc. other than Cancel/Close,etc. on Click of Save button u can change the hidden control value to "No".
If the user presses Close/Cancel button the u can explicity call the invalidate method by calling some jsp(which contained this invalidate process)
using the below js function.
But onUnload of the form page..then call a javascript fn..which checks for the hidden control value..for "Yes"..if true..then calls the jsp page doing invalidate process.
eg.
<BODY onUnload="javascript:_close();">
<SCRIPT>
function _close()
{
if(document.form.hdInvFlag.value=="Yes")
{
//This will submit to the invalidate page using GET method without refreshing the current page
var delImg = new Image();
delImg.src = "invalidate.jsp";
}
}
</SCRIPT>
Hope this is solves ur problem..
Naveen
sunitha reghu
Ranch Hand

Joined: Dec 12, 2002
Posts: 937
Originally posted by Bear Bibeault:

Incorrect. The session has no idea that the browser was closed. It will timeout (after whatever period is specified on the server) if no further activity takes place, but the session will not automatically invalidate upon browser close.
hth,
bear

when thw user closes the browser and then opens a new window another session will be created ie wat i meant by that.
Manas Ahlaad
Ranch Hand

Joined: Nov 07, 2001
Posts: 165
Hi Sunita
I think , thats not what you meant. When a user closes windows , session still remains .
when he opens a new windos , new session is created but the old session still REMAINS active.
This is a topic which has got no solution ideally.
Thanks
Manas
 
 
subject: calling a javascript function on closing browser
 
Threads others viewed
invalidate
How to track browser close
Delete Records When user clicks close button of a browser
Closing the browser window through Java
JSP Session and validation
MyEclipse, The Clear Choice

cast iron skillet 49er

more from paul wheaton's glorious empire of web junk: cast iron skillet diatomaceous earth rocket mass heater sepp holzer raised garden beds raising chickens lawn care CFL flea control missoula heat permaculture