Firing a Code on Window Close

let me explain the process in steps so that it is easier for everything to understand everything - the following are the steps done by the users in my application :

  1. Login to the application using their username and password
  2. go to the home page
  3. Click a Link to open a window in a pop up box
  4. as soon as the pop up window opens i enter the start time into the database
  5. there is a end sesison link in the pop up window which the user can click and close the window
  6. when the user clicks the end session window - i send an ajax request to enter the closing time and then close the window
  7. now the life is easy when the user clicks the window using the link but if the user closes the window , or right clicks on the browser and uses close then at times in some browsers the code doesnt fire

SO what is the solution to this - what i want to assure is that each time the window is close with whicih ever way - my ajax function should be fired in any condition and all the browsers. My focus is on IE 7 , 8 , Chrome and FF

Is there a way to check if a person has javascript enabled so you can be 100% sure? Maybe using PHP? I know that javascript can be enabled upon page load, and then disabled after that… but just wondering if it’s possible to check if js is enabled at one point in time, and be 100% sure if it is or isn’t (at that point in time).

That will be the JavaScript onbeforeunload event that you’re wanting there.

onbeforeunload is a proprietary event and is often blocked because pages use it to pop up annoying “before you leave” ads.

The best way to do this is to use a heartbeat ajax script where the page sends a message to the server every minute or so to let the server know the page is still open. You then run a cron job on the server at about two minute intervals that checks that the heartbeat ajax has run at least once since the cron job last ran and if it didn’t then the page must have been closed.

Felgall,

By proprietary event, do you mean that the user’s browser may or may not support the function depending on its user-configured settings / built in capabilities?

Yes. IE is the origin - I believe Firefox and webkit do as well. Opera does not. Many adblockers disable the event because nefarious scripts abuse the call.

Opera does support though the onunload event, but can that be used to fire the ajax event that the OP requires?

If not, then the heartbeat technique may be a last-ditch effort to fall back on.

Is there a handy link that contains this information regarding which browsers disable which functions? I didn’t know about this prior to reading in this thread.

All modern browsers support the standard events but as with all JavaScript all your visitors are free to override whatever parts of it they want to as any JavaScript they attach to their browser will override yours.

onbeforeunload is a proprietary event not a standard one which means that only some browsers support it at all. It is mostly used by scam sites to try to force you to stay on their site until they sell you something and so anyone who has a moderate knowledge of how their browser works will either disable it or will choose an alternative browser that doesn’t support that proprietary spamming event at all.

As I mentioned earlier there is a way of using only standard processing to achieve the desired logout if your users forget to and that is to use a heartbeat ajax script to do it.

Of course whichever way you do it that relies on JavaScript will fail if your visitor disables your script so you will need a server side only solution as a fallback - automatically logging everyone off who is still logged on say ten minutes after they last requested a page while logged in might be a reasonable fallback for when your visitors don’t allow a JavaScript solution.

Yes - try to run some JavaScript at that point. If it runs then JavaScript is enabled.