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

Knowing when a window is really closing? 1

Status
Not open for further replies.

davem99

Programmer
May 19, 2003
74
US
I want to put some OnUnload code in my ASP page so that I popup another windows that cleans up my session and updates my logout in a DB.

But, a POST or an 'F5' to refresh triggers this event.

Any idea how to work around it?

Thanks all!
 
Are you just trying to end a user's session and close any database operations? If so, then you can use the Session_OnEnd in the global.asa file. This file would be stored in the root directory of your website application, but is not mandatory to be there.

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
I need to reset a column in my table that is set when they login.

I have code in the Session_OnEnd event that says:

Update mytable set Connected=0 where ID=Session("memberID")

Unfortunately, it seems that this doesn't cut it - perhaps because the memberID session var goes on session end?

I dunno.
 
Session ends after the specified last interation time (set in IIS > site properties > Directory > Configurations > app object - Session timeout) with the server. That means Session_OnEnd fires after a set time when the browser close.

<body onunload=&quot;JavaScript:location.href='logout.asp'&quot;>

But u can make both happen at once u can call a intermediate ASP page when you are loging out or closing the page. and in this page u can code like this

<% Session.Abandon %>

or any other database logic

this will force the session to end and will fire Session_OnEnd


regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
Thanks, Brian.

I have a function that pops up an small window and tells the user they're being logged out and it closes.

Unfortunately, it looks like unload fires on refresh or POST so it kicks my user out pretty much whenever they do anything :(

Any ideas?

thanks!

 
How 'bout something like this?

<script language=&quot;javascript&quot;>
var newWindow;

function openNewWindow(URLtoOpen, windowName, windowFeatures) {
newWindow=window.open(URLtoOpen, windowName, windowFeatures);
}

function closeWin () {
if (newWindow != null) {
newWindow.close();
}
}
</script>

<BODY onUnload=&quot;closeWin()&quot;>

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
I'm sure if my Java was better than my Russian (which is zero!) I could attach to the window close event and fire my own thing.

Right now though.....I remain puzzled and confused! ;)
 
Thanks Brian, I think we posted at the same time above so my comment makes more sense after the previous post!

Here's what I have:

<script LANGUAGE=&quot;JavaScript&quot;>
<!--
function LogOff()
{
window.open('autologoff.asp', 'Window1', 'resizable,height=130,width=260');
}
//-->
</script>


<body onunload=&quot;LogOff();&quot;>


Again, all is fine except f5 and POST triggers unload too
 
Ok, here is an example that will take care of the refresh and form post. It will determine if the window is being closed via the &quot;X&quot; in the upper right corner or a hyperlink (#2) that has the ClosesWindow set to true.

You can add a location.href in the CheckWindowClosed function.

<html>
<head>
<Script language=&quot;javascript&quot;>
var ClosesWindow = true;

function CheckWindowClosed() {
// This code only fires when the window is closed when the
alert('Page Closed');
}
</script>
</head>
<body onBeforeUnload=&quot;if(ClosesWindow == true){CheckWindowClosed();}&quot; onmouseover=&quot;ClosesWindow=false;&quot; onmouseout=&quot;ClosesWindow=true;&quot;>
<a href=&quot;&quot;>Does not count as a window close</a> <br>
<a href=&quot;&quot; onmousedown=&quot;ClosesWindow=true;&quot;>Counts as a closed window</a>
</body>
</html>

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
Hey Brian - I like the way you think but there is a problem when the mouse is out of the main window and you hit F5.


Man...this is killing me!!!
 
hmmm...maybe we can capture the screen coordinates and see if the mouse is outside the main window when the &quot;Refresh&quot; is performed. Let me do a little research on this too. I will post back in a few.

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
Thank Brian - don't you think though that this is something that should be easy?!?!?!?

They can send a gizmo to Mars..!

thanks again - I appreciate it!
 
OK....I found (but not yet tested) 2 options....

1) This is IE only apparently

function onClose()
{
if ((window.event.clientX<0) && (window.event.clientY<0))
alert(&quot;you closed me&quot;);
}
...
<body onunload=&quot;onClose()&quot;>


and

2) Use frames and dump the close logic in a frame that isn't visible so won't be impacted by POST or refresh.....


<html>
<head>
<title>frames example</title>
</head>
<noframes>To be viewed properly, this page requires frames.
</noframes>
<frameset rows=&quot;100%, *&quot;>
<frame src=&quot;dummy.html&quot;>
<frame src=&quot;leave.jsp&quot;>
</frameset>
</html>

<html>
<head>
</head>
<body onunload=&quot;leave()&quot;>
<!-- body onbeforeunload=&quot;leave()&quot; -->here
</body>
</html>
<script language=&quot;JavaScript&quot;>
function leave() { document.location.href=&quot;</script>

still seems like a pain in the &%$ though.....
 
Yeah, I don't think there is a really clean way of doing this, although, I sort of like the frames method as you described.

Did you try posting in the JavaScript forum since this has much to do with JavaScript, more than ASP.

I suppose you could also give the user a confirmation window (then redirect if necessary) and let them decide if they would like to logout. I know that this would be annoying when they click on the refresh button, but in your application, do you anticipate the refresh button will be clicked a lot?

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
Yes - I did post this to the Java peeps too.

The frames are kindof cool approach to the problem but something just keeps bugging me that there HAS to be an easy way!!

The Confirm? would annoy the hell out of my users since OnUnload fires on POST and my app is full of self-POSTing forms.

Thanks again for your help on this!

-dave
 
Thanks for the star and no problem. Have you considered posting to a separate ASP file and redirecting back after you processed the information? Would this alleviate any issues?

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
Alas that POST action still raises the dreaded OnUnload event....

I'll keep plugging away and see what else surfaces from the Java folks.

Thanks again, Brian!
 
Cool, hope you find what you need. If I find something I will be sure to post back.

regards,
Brian
The problem with doing something right the first time is that nobody appreciates how difficult it was.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top