Click to See Complete Forum and Search --> : Can i detect if user is leaving my site??


aoife
09-26-2006, 04:51 AM
I was wondering if anybody could help me with the following.....

In my asp site, a unique user session is created each time a user logs on and this session is expired after an hour. However i need to be able to delete the session whenever a user clicks away from the site (ie. by clicking x, IE back button, file->close etc.)

Is there any way i can detect if the user is going to close my site or navigate away from it so that i can call the function to delete the session from the database?????

RetroFiesta
09-26-2006, 06:51 AM
I don't think you can do it from closing the window, but you can do it when the URL changes. ( I just can't remember how ) :(

You'll of seen the multiple pop-ups you get from certain 'entertainment?!' sites I'm sure. Basicaly, the same way as that.

Have a look for scripts that pop up a 'Are you leaving' sort of message.

Hope that's of 'some' help to you.

Jason.

Charles
09-26-2006, 08:58 AM
This doesn't work for those of us that don't use JavaScript and someone navigating with the URL will always look like someone leaving your site but:onload = function () {
var l, i = 0
while (l = document.links[i++]) {onclick = function () {self.onunload = function ()}}
}

onunload = function () {
// tell the server that the user that Elvis has left the building.
}That example only works if all of your links are local, but you get the idea.

Kor
09-26-2006, 09:47 AM
I think onbeforeunload is a better crossbrowser choise as an event....

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>Untitled Document</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Style-Type" content="text/css">
<meta http-equiv="Content-Script-Type" content="text/javascript">
<script type="text/javascript">
window.onbeforeunload = function (evt) {
var message = 'Some of the form\'s data might not be submitted yet!';
if (typeof evt == 'undefined') {
evt = window.event;
}
if (evt) {
evt.returnValue = message;
}
return message;
}
</script>

</head>
<body>
</body>
</html>

Dok
09-26-2006, 01:22 PM
unbeforeload is not standardized though...

felgall
09-26-2006, 02:42 PM
onunload wont have enough time to pass the info to the server if the person leaves the page by closing the browser.

onbeforeunload is nonstandard and so is not understood by most browsers.

Why not set up a heartbeat type of thing where the page passes info (via Ajax) at regular intervals while it is still open. When the server stops receiving the hearbeat signal then it knows they have left the page. You just need to add a workaround for those without Javascript.

Kor
09-27-2006, 01:04 AM
unbeforeload is not standardized though...
onbeforeunload is not standard, but at least works both in IE and Moz, which will cover most of the browsers. onunload is not crossbrowser and acts too quickly...

felgall
09-27-2006, 01:46 AM
onunload is the only standard cross browser event triggered when (after) a page is unloaded from the browser.

Don't rely on Javascript for anything crucial to your functioning. Using onbeforeunload will only work perhaps 85% of the time as the rest of the time either Javascript is turned off or the browser doesn't support that non-standard event.

sridhar_423
09-27-2006, 04:35 AM
:confused: :(
while (l = document.links[i++]) {onclick = function () {self.onunload = function ()}}
i hav two doubts here
1. l.onclick (If i'm not wrong, you accidentally typed it as "onclick" without any prefix.. right?)
2. how is tat function implemented?(l.onclick=function()) I hav put one alert in tat. IE doesn't recognise tat. It only executes the code in unload=function(){//} (outside while loop)

I tried but cudn't get the idea. is the function defined by the second "unload" not suffiicient? I guess it'll be called whenever the user navigates away from the page.. by clicking on the link or by typing a different address in the addr. bar.
Can u pls explain.

Thanks,
Sridhar

aoife
09-27-2006, 07:59 AM
Hi everyone -> thanks for all the help !!

Think the above only works if the user is leaving a webPage and not a webSite....

Have found a solution which shows a message every time unless it is an internal link (i.e format all the links in the site to bypass the message) This however still does not work if the user clicks refresh/back/forward buttons i.e if they click back button and they are navigating to a page which is within my site they will still be displayed the message asking them if they want to leave the site.........

aoife
09-27-2006, 09:39 AM
Why not set up a heartbeat type of thing where the page passes info (via Ajax) at regular intervals while it is still open. When the server stops receiving the hearbeat signal then it knows they have left the page. You just need to add a workaround for those without Javascript.


Is there anywhere where this is explained - tried searching for it but the ajaxpatterns.org site seems to be down :(

Thanks a million
Aoife

aoife
09-28-2006, 03:43 AM
Hi all,

Finally found a way to detect if user is leaving your site!!!
It involves using a frameset which only has 1 frame -> the frame which will contain your site. Instead of popping up a message when each page in your site unloads, pop-up the message when the frameset unloads (i am using on before unload!) Because all of the internal pages of your site will be loaded into the frame (frameset is not unloaded each time) then message will not appear.

This works great and is simple as it involves adding only 1 file to the site to get this working properly

1 problem though -> the frameset is unloaded when the user clicks the refresh button (so message asking if you want to leave the site appears evwen though you are only refreshing the page)

So have to now try to find out how to detect a refresh to stop this from happening ... let me know if you have any suggestions

Thanks for everyones help :)