 |
| Javascript How-To Ask your "How do I do this with Javascript?" questions here. |
Welcome to the p2p.wrox.com Forums.
You are currently viewing the Javascript How-To section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
|
|
|
|

April 27th, 2004, 02:32 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Detect when window closes, not unload
I would like to call a function to log the client out when he/she closes the window. I do not want to call the function when navigating away from my page, only when the window closes. Does anyone know a way to do this?
Thanks ahead of time,
Snib
<><
|
|

April 27th, 2004, 03:52 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
Hi Snib,
I don't think you can do that easily. As far as the browser is concerned, there is no difference between unload being fired when the user browses away, or when the user closes the browser.
I was thinking about storing a variable in a parent frameset, but after a bit of thinking, that doesn't seem to work either.
Why do you need to make the distinction? Isn't the end result for your application the same? The user no longer wants to view your page?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: I Am The Cosmos by This Mortal Coil (Track 20 from the album: Blood) What's This?
|
|

April 27th, 2004, 04:11 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Imar,
The page is in a popup window without toolbars or status bars. This page is refreshed occasionally, which would trigger an onunload event every time. When the user leaves (by closing the page) instead of clicking 'Log Out,' which tells the server the user is leaving before closing the page, the server assumes the user is still logged in.
Let me know if you need more of an explanation.
Thanks,
Snib
<><
|
|

April 27th, 2004, 04:20 PM
|
 |
Wrox Author
|
|
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
|
|
In that case, a nested page in a frameset might do the trick.
Add the onunload handler to the frame set page only. OnUnload will only fire for the frameset page when the user closes the browser.
The child page can refresh at will without causing an onunload of the parent.
Does that help?
Imar
---------------------------------------
Imar Spaanjaars
Everyone is unique, except for me.
While typing this post, I was listening to: Almost Over by Limp Bizkit (Track 6 from the album: Results May Vary) What's This?
|
|

April 27th, 2004, 04:23 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
I will try it and tell you how it works. It sounds good.
Thanks Imar,
Snib
<><
|
|

April 27th, 2004, 05:49 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Thanks Imar. It works perfectly.
Thanks again,
Snib
<><
|
|

May 5th, 2004, 07:47 PM
|
|
Registered User
|
|
Join Date: May 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Imar and Snib,
I am working on a similar task. I want open a popup only when user close windows or leave my site, but not refresh the site. I tried your frameset method, as follows, but unfortunately it still has popup window when reload. Did I miss anything?
<html>
<head>
<title>My Document Title</title>
</head>
<frameset cols="30%,*" onUnload="javascript:alert('First Action');">
<frame src="left.html" name="list">
<frame src="right.html" name="body">
</frameset>
</html>
If not using frameset, is there a way to detect the person is leaving my site instead of refreshing or still surfing my site?
Thank you very much,
-Alan
|
|

May 5th, 2004, 08:07 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
In my case, the site was in a popup window, which excludes the refresh button. I had a JavaScript that automatically refreshed a certain frame. Pressing Refresh or F5 refreshes the frameset, which unloads it and then loads it again, triggering the unload event. Like Imar said, you probably won't come by a way to do this easily. I'll let you know if I find something on it, but I wouldn't count on it if I were you :(....
Snib
<><
|
|

May 5th, 2004, 08:09 PM
|
|
Friend of Wrox
|
|
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
|
|
Perhaps you should consider popping your site up in a window like I did and excluding the toolbars. Then the onunload method would work.
HTH,
Snib
<><
|
|

September 8th, 2004, 07:34 AM
|
|
Registered User
|
|
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
|
|
Hi snib,
I am stuck with a problem, i have a base class - PageBase. vb
all the aspx pages are inherited from the base class, my objective is to update a database flag when the user logoffs from the site, now the life becomes difficult when the user clicks on close(x) instead of logoff button. i have attached window.onunload event to the base class but when during current session,user opens history /favourites on the left frame and clicks on the back button, window closing event (i had placed an alert to check for the event) is triggered and even though user is in site - his session is expired forcibly due to the code in window.onunload().
Please suggest.
Thanks in advance..
Murali
|
|
 |