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
- <html>
- <head>
- <script language="javascript">
- var altKey = false;
- var keyCode = 0;
- function closeSession(evt){
- evt = (evt) ? evt : event;
- clickY = evt.clientY;
- altKey = evt.altKey;
- keyCode = evt.keyCode;
- if(!evt.clientY){
- // Window Closing in FireFox
- // capturing ALT + F4
- keyVals = document.getElementById('ffKeyTrap');
- if(keyVals.value == 'true115'){
- return 'close 1';
- }
- if(keyVals.value == ''){
- // capturing a window close by "X" ?
- // we have no keycodes
- return 'close 2';
- }
- } else {
- // Window Closing in IE
- // capturing ALT + F4
- if (altKey == true && keyCode == 115){
- alert('close 1');
- // capturing a window close by "X"
- } else if(clickY < 0){
- alert('close 2');
- // simply leaving the page via a link
- } else {
- //alert('close 3');
- return void(0);
- }
- }
- }
- function whatKey(evt){
- evt = (evt) ? evt : event;
- keyVals = document.getElementById('ffKeyTrap');
- altKey = evt.altKey;
- keyCode = evt.keyCode;
- if(altKey && keyCode == 115){
- keyVals.value = String(altKey) + String(keyCode);
- }
- }
- window.onkeydown = whatKey;
- window.onbeforeunload = closeSession;
- </script>
- </script>
- </head>
- <body>
- <a href="http://rtscntl/">test</a>
- <input type="text" id="ffKeyTrap"><br />
- </body>
- </html>