473,503 Members | 1,706 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

FireFox and onbeforeunload - detect close event

1 New Member
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 33085
acoder
16,027 Recognized Expert Moderator MVP
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
3760
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
2089
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
11207
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
17023
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
4714
by: bloodandrose | last post by:
Can you help me How to detect browser close event? Thanks
5
34348
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
26491
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
4106
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
2529
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
7083
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
7278
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,...
0
7328
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
5578
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,...
1
5011
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...
0
3166
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
1510
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...
1
734
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
379
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence...

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.