View Full Version : onUnload question!
dc2 jspec
09-20-2002, 04:24 PM
Ok, I have a website with a login. I am trying to log a user out when he/she closes the window with the "X". I got the onUnload to work properly, but I DONT want this to happen if I am just surfing to another page within the site. As of right now it does. For instance, if I am on first_page.php and I click a link to second_page.php, it logs me out. <==I dont want this, I only want this to happen when the "X" is clicked! How can I stop this from happening? thanx in advance.
A1ien51
09-20-2002, 05:03 PM
the onunload event occurs on every event leaving th page. What has to be done is a more complicated script.
Easy Solution is a session cookie. I would recommend that, it gets deleted when all of the browser windows are closed.
Or you can do t the hard way, have to have flags for each link and check for flags when the page is onloaded. But this means if the back button is pressed or if the refresh is pressed, then it thinks that the x was hit. So you are out of luck
dc2 jspec
09-20-2002, 05:03 PM
I have a session running right now. You see, I don't want to have a new pop up window open every time a link is clicked to check and see if the user is clicking the X or if the user is clicking a link. My code is similar to the following:
second_page.php
<body onunload=(open logout.php)>
logout.php
-logs the user out.
I WANT SOMETHING LIKE THE FOLLOWING, I just don't know how to do it. And I have the sessions right now running.
second_page.php
<body onunload=(if the browser is closed(open logout.php))>
logout.php
-logs the user out.
-----DOES THIS HELP ANY...?
A1ien51
09-20-2002, 06:54 PM
Have you looked at server side for this solution?? Try there for an easier solution.
Now for the client side solution
This is messy as can be and it does not catch the refresh button or the back button, so it will log people out even if they are still on the site.
the code:
<html>
<head>
<title>Exit Script</title>
<script>
Leave=1;
function ExitIt(x,y){
if(Leave==1&&x==1){
alert('Leaving');//Your Code Goes here for the log out
}
else if(x==0){
Leave=0;
document.location.href=y;
}
else {
Leave=1;
}
}
</script>
</head>
<body onunload="ExitIt(1);">
<a href="javascript:ExitIt(0,'http://www.a1ien51.8k.com/scripts/popupwingenV2.htm')">A1ien51.8k.com's Pop Up Generator</a><br>
<a href="javascript:ExitIt(0,'http://www10.brinkster.com/A1ien51')">New Site</a><br>
</body>
</html>
dc2 jspec
09-20-2002, 07:09 PM
hmmm....that is the best that I have heard thus far, but like you said, it leaves too many options not handled. Can you give me any suggestions on how to go about this server side? It is a PHP script. thanx again.
vBulletin® v3.8.2, Copyright ©2000-2012, Jelsoft Enterprises Ltd.