...

View Full Version : user closing browser event?



nikko50
12-27-2010, 08:54 PM
Is there a way to capture the event of the user closing the browser??

Old Pedant
12-27-2010, 09:01 PM
Not really. You can capture the onunload but that can happen for many reasons, including even simply moving to another page on your own site. And there's really no guarantee the onunload will occur if, for example, the user just kills the browser process from the task manager (or maybe turns off the power switch or unplugs the network cable or...).

nikko50
12-27-2010, 09:11 PM
Does it have to be used with the BODY tag or can I use it elsewhere. Can you maybe provide a small example.

Tracy:)

Philip M
12-27-2010, 09:37 PM
Does it have to be used with the BODY tag or can I use it elsewhere. Can you maybe provide a small example.

Tracy:)

Wel, you can only onload the whole page, not a a part of it!

<body onunload = 'alert("Thank you for visiting my site!")'>

But as Old Pedant says, it is far from reliable. And some people see that sort of thing as intrusive, with overtones of being monitored. Not recommended generally.

Old Pedant
12-27-2010, 09:47 PM
You can, of course, simply attach it in the JS code. But what you need to do is carefully exclude normal navigation so that the user doesn't get an annoying message when, for example, submitting a <form>.

And there is also onbeforeunload which might be more useful.

Example at http://www.plopon.com/byebye.html

And the code for it:


<html>
<head>
<script type="text/javascript">
var trap = true;

function byebye( )
{
if ( ! trap ) return null;
return "Are you sure you really want to leave my lovely site???";
}
window.onbeforeunload = byebye;
</script>
</head>
<body>
<a href="rollone.html" onclick="trap=false;return true;">Go here on my site</a>
<hr>
<a href="http://www.cnn.com">Go away</a>
</body>
</html>



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum