472,122 Members | 1,507 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,122 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 32768
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

Post your reply

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

Similar topics

1 post views Thread by abhay | last post: by
2 posts views Thread by Tony Clarke | last post: by
1 post views Thread by bloodandrose | last post: by
5 posts views Thread by jimmy | last post: by
11 posts views Thread by =?Utf-8?B?QWxoYW1icmEgRWlkb3MgS2lxdWVuZXQ=?= | last post: by
reply views Thread by leo001 | last post: by

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.