Wrox Programmer Forums
Go Back   Wrox Programmer Forums > Web Programming > JavaScript > Javascript How-To
|
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
 
Old April 27th, 2004, 02:32 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default 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

<><
__________________
-Snib - http://www.snibworks.com
Where will you be in 100 years?
 
Old April 27th, 2004, 03:52 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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?

 
Old April 27th, 2004, 04:11 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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

<><
 
Old April 27th, 2004, 04:20 PM
Imar's Avatar
Wrox Author
 
Join Date: Jun 2003
Posts: 17,089
Thanks: 80
Thanked 1,576 Times in 1,552 Posts
Default

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?

 
Old April 27th, 2004, 04:23 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

I will try it and tell you how it works. It sounds good.

Thanks Imar,

Snib

<><
 
Old April 27th, 2004, 05:49 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

Thanks Imar. It works perfectly.

Thanks again,

Snib

<><
 
Old May 5th, 2004, 07:47 PM
Registered User
 
Join Date: May 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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
 
Old May 5th, 2004, 08:07 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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

<><
 
Old May 5th, 2004, 08:09 PM
Friend of Wrox
 
Join Date: Nov 2003
Posts: 1,285
Thanks: 0
Thanked 2 Times in 2 Posts
Default

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

<><
 
Old September 8th, 2004, 07:34 AM
Registered User
 
Join Date: Sep 2004
Posts: 1
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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







Similar Threads
Thread Thread Starter Forum Replies Last Post
delete session when tab window closes bikermike Beginning PHP 2 February 21st, 2009 05:02 PM
on window unload event in javascript. babu17 Classic ASP Basics 1 April 15th, 2006 05:48 AM
How to detect if pop up window is disabled bekim Javascript How-To 1 January 23rd, 2005 06:38 AM
Handling window unload event MurthyGarimella Javascript 2 June 30th, 2004 10:59 AM
How to detect window Close (X) Button cyberjames2003 Javascript How-To 2 July 27th, 2003 08:13 PM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.