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 TouchToneTommy on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Trapping for Browser Window close using the X 1

Status
Not open for further replies.

MaeJams

Programmer
Apr 24, 2001
51
US
I posted this on the ASP forum and someone suggested I try this one...so here goes...

Does anyone know a good way to trap for the Browser Window closing? I need to put some additional "logging-off" code in a suitable location to log users off of a server (other than the web server). I can't put this code in the Window_OnUnload of the main page in my web app, as this fires anytime the page reloads with new content. I have the code duplicated in the Session_OnEnd of the Global ASA file, but that event does not fire when the Browser Window is closed using the X button.
 
I am running into the same issue. The resolution I was in the process of trying is
window.captureEvents(window.onUnload)...not having any luck yet.

If that doesn't work I am going to use the Session_OnEnd in the global ASA. the code I need to run doesn't have to happen immediately so it's ok that it runs when the session times out. I'm not sure it will work but I can let you know if you want.
 
If you are writing an intranet that uses only the IE5+ browser, then you can use the unbeforeunload event. (see
It lets you run a javascript function right before the page closes. If that function has a return value that is a text string, then that string will appear in a special dialog box, giving the user the opportunity to stay on the page or continue to unload the page. That is the only difference between onunload and onbeforeunload: giving the user the opportunity to cancel and remain on the page. That's as close as you're going to get to actually trapping the window close behavior, without a special signed script, using some activeX, or somesuch.

In your case, though, you could just use onunload to run a function to open another small window, which opens and then closes instantly with javascript, and it can run the code to complete the logout:

<HTML>
<HEAD>
<SCRIPT>
function closeIt()
{
window.open(&quot;logout_page.asp&quot;);
}
</SCRIPT>
</HEAD>
<BODY onunload=&quot;closeIt()&quot;>
HTML content</BODY>
</HTML>

The good thing is the above script will work for all browsers.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top