View Single Post
  #4 (permalink)  
Old March 22nd, 2009, 04:34 PM
Old Pedant Old Pedant is offline
Friend of Wrox
Join Date: Jun 2008
Location: Snohomish, WA, USA
Posts: 1,649
Thanks: 3
Thanked 141 Times in 140 Posts

You can't really determine the difference between a true browser close and navigating to the next page or a completely different web site.
Not strictly true.

It's a pain, but you *CAN* make that determination.

Here is a very *SIMPLE* demonstration:
var goingAway = true; // assume the worst

function sayGoodbye( )
    if ( goingAway ) alert("Leaving so soon?  Sorry to see you go.");
<body onunload="sayGoodbye();">
<a href="">visit google</a>

<form action="anotherPageOnThisSite.html" onsubmit="goingAway=false; return true;">
Give me your name: <input name="name">
<input type=submit value="Log in">
Do you see it?? You just need to make sure that EACH AND EVERY WAY that a person might navigate to another page on your site sets the goingAway flag to false and then the logic in ONUNLOAD (and it could be in ONBEFOREUNLOAD) can ignore those moves while capturing all others.

Now, it's still not perfect. The user can simply kill the browser (e.g., with the task manager) or could lose connection (modem goes dead) and you'll never *get* the unload event. So you still should have the backup of Session.OnEnd in global.asa or equivalent. But used with care you *can* capture the majority of all cases you need to catch.
Reply With Quote