By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
445,929 Members | 1,830 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 445,929 IT Pros & Developers. It's quick & easy.

FireFox and onbeforeunload - detect close event

P: 1
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
Share this Question
Share on Google+
1 Reply


acoder
Expert Mod 15k+
P: 16,027
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.