473,654 Members | 3,251 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 33138
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
3775
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. regards casper
2
2101
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 message buy is there anyway of completely stopping a user from closing a window. Thanks, Tony.
3
11216
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 written on the client browser , like IE. To send a POST to web server to acknowledge its close event. However, I just think, web server and client browser is a TCP connection. And if the client browser is close and it imply the TCP socket is...
12
17044
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 is being closed? It is easy enough if the user selects "exit" from this tray icon but how can you detect if Windows is closing the program down? Normally I would simply do something in the Form.Closing event but without a form how can you...
1
4725
by: bloodandrose | last post by:
Can you help me How to detect browser close event? Thanks
5
34359
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 helps to recognise whether the close button is clicked( also differentiates whethet refresh was clicked). The problem arises whenever i try to close the window by resizing it or by right clicking at the bottom of the page and then closing it. Can...
4
26631
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 using this code but I did something wrong because the temp function is fired every time the parent page loads: function temp(){ alert('the iframe was closed'); }
11
4121
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, etc ? Thanks. -- http://www.alhambra-eidos.es/web2005/index.html www.kiquenet.net
2
2535
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 call popup to execute it on unload of the page. there are 2 problems that window.open() is not working firefox and also I have to make the changes to more than 100's of pages. So Please if anyone has Idea or can help me It will be aprreciable. ...
0
8376
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
8290
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
8708
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
7307
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6161
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5622
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4149
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 last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
1
2716
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 we have to send another system
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.