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

Page Close Event? 1

Status
Not open for further replies.

stevenk140

Programmer
Nov 20, 2003
34
CA
Is there some event that lets me know when the page has been closed. My problem is I want to delete all files that were created during a viewing of a web page. What is the best way of doing this?

Steve
 
steven: The best way would be to have the window closed by an object - however, if you want to detect the close of the window when a user clicks on the "X" close button of the window, that would require a diff approach. An alternative is to have the files cleared after a Session timeout, etc... I'll follow up on this with a little research - again, I think this is a great question, "identifying the close event" and subsequently carrying out some code routine.
 
In "classic" ASP I would execute a javascript finction on window.onunload event. The function would open another asp page, which has some server side script, performing clean-up and closing the page.
Code:
somepage.asp:
<html>
<head>
 <script language=&quot;javascript&quot;>
  function doCleanUp()
  {
   window.open(&quot;cleanup.asp&quot;, &quot;&quot;, &quot;top=2000&quot;);
  }
</script>
</head>
<body onunload=&quot;javascript:doCleanUp();&quot;>
........
</body>
</html>
Notice top=2000, the new window will not be visible, just a short flash will occur.

cleanup.asp:
<html>
<body onload=&quot;javascript:closeMe()&quot;>
<%
 'execute server side script to perform clean up...
%>
<script language=&quot;javascript&quot;>
 function closeMe(){
  self.close();
 }
</script>
</body>
</html>
This works pretty good in classic ASP, haven't tried anything like this in ASP.Net though. Also keep in mind that the onunload event will fire not only when the user clicks the &quot;x&quot; button, but while navigating out of the page as well, like clicking on a redirecting link. BTW, Isadore's idea to perform the clean up in Global.asax inside the Session_End handler looks much better and cleaner, you should probably take a closer look at it.
 
Thanks for the tips guys, this now leads me to another question.

I have decided to go with Isadore's approach and do a clean up at the end of a session. My question now is, how do I see when the function

protected void Session_End(Object sender, EventArgs e)
{
//code ...
}

gets called? It never seems to hit my break point when I close the window or anything.

Does this code get executed on session timeout?

Can I debug this code some way?

Thanks for the help so far,

Steven
 
Steven,

Yes, The Session_End will get called on session time out. Usually 20 minutes. This can be debugged by either waiting for the 20 minutes to time out, or you could set your timeout to something like 1 minute for testing.

ComaToast
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top