Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Log off time

Status
Not open for further replies.

jack1971

Programmer
May 23, 2002
14
CA
I was supposed to record the log-off time for users. We can do it by creating a log-off button to do the record. What if user simply ignores it and closes the browser?

Thanks in advance for any advice. A piece of sample codes will be more helpful.

Jack
 
Hi Jack!

This can be done as follows using javascript:

To the page with the logoff button, add the following:

In the <head> section:
Code:
<script language=&quot;JavaScript&quot;>
	
	function logout()
	{
		window.open('logout.jsp?logout=1', 'logoutwindow', '');
	}
	
</script>

In the <body> tag:
Code:
<body onunload=&quot;logout();&quot;>

logout.jsp is a file that logs the user logoff time when it is requested from the server. (you can of course use your own file if you want.)

In the logout.jsp <body> tag, add the following:
Code:
<body onload=&quot;window.close()&quot;>
This will make the window close when it is done loading.

So now when the user closes his browser window, a new window will open, requesting the logoff page, and then close as soon as it is loaded.

Hope this helps.

regards,
Blaxo
 
you can also do this by using the event onbeforeunload of the HTML tag.. but this event i think is only implemented on the IE 6.. this is more usefull if you need to do more especific things when the user close the page.. or you need more control on the log out event. this way you can stop the page from closing,redirect to other place, etc.
 
The script I proposed doesn't seem to work right, since it calls the function logout() every time the page is changed, not only when the browser window closes... it only works right when your web application is in a frameset page that never changes, and the
Code:
onunload=&quot;logout();&quot;
is in the
Code:
<frameset>
tag.

regards,
Blaxo
 
Thanks, guys!

Those gave me enough thoughts!

Jack
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top