473,419 Members | 1,704 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,419 software developers and data experts.

FireFox and onbeforeunload - detect close event

I'm working on a web app that under normal circumstances will be happy with the Session_OnEnd event, but when a user leaves a page by closing the browser by either Alt+F4 or just hitting the "X", I'll need to kill the session.

Now, with the onbeforeunload event, I can handle this quite easily in IE, but in FireFox, it's another matter. For one thing, FireFox seems to empty out its event object on a page unload, so it's very hard to track keystrokes, etc.

Anyway, so far I've got some decent code going, but neeed a little help. If there ANY way to test in FireFox when the browser is being closed? In IE, we can get clientY and if it's < 0, then the user is most likely clicking the close button.

Also, is there a way to test in either browser what the target url is when the page is unloading? Thanks for your help!

Current Code:
Currently works in IE for:
Alt+F4 keystroke, kill button click, and knows when the page is being unloaded (but not to where)
Works in FireFox for: Alt+F4 keystroke (with some rigging) and knows when the page is being unloaded ( but not by kill button or nav away from site)
Expand|Select|Wrap|Line Numbers
  1. <html>
  2. <head>
  3. <script language="javascript">
  4. var altKey  = false;
  5. var keyCode = 0;
  6.  
  7. function closeSession(evt){
  8.  
  9.     evt = (evt) ? evt : event;
  10.  
  11.     clickY  = evt.clientY;
  12.     altKey  = evt.altKey;
  13.     keyCode = evt.keyCode;
  14.  
  15.     if(!evt.clientY){
  16.         // Window Closing in FireFox
  17.         // capturing ALT + F4
  18.         keyVals = document.getElementById('ffKeyTrap');
  19.         if(keyVals.value == 'true115'){
  20.             return 'close 1';
  21.         }
  22.  
  23.         if(keyVals.value == ''){
  24.             // capturing a window close by "X" ?
  25.             // we have no keycodes
  26.             return 'close 2';
  27.         }
  28.  
  29.     } else {
  30.         // Window Closing in IE
  31.         // capturing ALT + F4
  32.         if (altKey == true && keyCode == 115){
  33.             alert('close 1');
  34.         // capturing a window close by "X"
  35.         } else if(clickY < 0){
  36.             alert('close 2');
  37.         // simply leaving the page via a link
  38.         } else {
  39.             //alert('close 3');
  40.             return void(0);
  41.         }
  42.     }
  43. }
  44.  
  45. function whatKey(evt){
  46.     evt = (evt) ? evt : event;
  47.     keyVals = document.getElementById('ffKeyTrap');
  48.     altKey  = evt.altKey;
  49.     keyCode = evt.keyCode;
  50.     if(altKey && keyCode == 115){
  51.         keyVals.value = String(altKey) + String(keyCode);
  52.     }
  53. }
  54.  
  55. window.onkeydown      = whatKey;
  56. window.onbeforeunload = closeSession;
  57. </script>
  58.  
  59. </script>
  60. </head>
  61.  
  62. <body>
  63. <a href="http://rtscntl/">test</a>
  64.  
  65. <input type="text" id="ffKeyTrap"><br />
  66.  
  67. </body>
  68. </html>
Nov 6 '06 #1
1 33072
acoder
16,027 Expert Mod 8TB
I'm working on a web app that under normal circumstances will be happy with the Session_OnEnd event, but when a user leaves a page by closing the browser by either Alt+F4 or just hitting the "X", I'll need to kill the session.
Just use a session timeout instead.

If there ANY way to test in FireFox when the browser is being closed? In IE, we can get clientY and if it's < 0, then the user is most likely clicking the close button.
I don't think there is. Just use onbeforeunload for all unload events.

Also, is there a way to test in either browser what the target url is when the page is unloading?
No, there isn't, AFAIK.
May 28 '08 #2

Sign in to post your reply or Sign up for a free account.

Similar topics

1
by: abhay | last post by:
Hello all, I want to trap the window.close() event when the user clicks on the close button of the browser using javascript. Can anyone shed light on this problem ? Thanks in advance. ...
2
by: Tony Clarke | last post by:
This is brobably a newbie question but is there anyway to actually stop a windows close event and stop a user from exiting a page the wrong way. I know you can use the onbeforeUnload to display a...
3
by: Denon | last post by:
How to trap browser close event in SERVER side? I read a lot of forum message, it talk about onclose(), onunload() and even onbeforeunload() event. However, all of theses are based on javascript...
12
by: Patrick Dugan | last post by:
I have an vb.net application that is a module that uses a "application.run" in the sub main to start. There is no form involved (just a system tray icon) How can you detect when the application...
1
by: bloodandrose | last post by:
Can you help me How to detect browser close event? Thanks
5
by: jimmy | last post by:
Hi all, I want to capture the event when the browser's close button is clicked in an html page. I tried using the event.ClientX and event.ClientY property in the body unload event, and this...
4
by: Mike Scirocco | last post by:
I have an iframe that includes a button: <input type="button" value="close this window" onclick="window.close();" > I would like to detect the iframe close event from the parent window, I was...
11
by: =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post by:
Hi misters, I have an aplication web asp.net 2.0 and I am Trying to detect the close event in browser, which is the best performance for do this ? What's up with Alt+F4, Refresh, user clicks X,...
2
by: heminfotech | last post by:
Hi I would like to update the status of users of my site. I am not getting any clue as to how execute a script on close event of the browser. I have tried one thing that is I have used to...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
1
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.