Detecting when someone leaves the website

<body onbeforeunload=“showUnloading(event);” > – put on body

–Javascript

var isClicked=false;
$(function() {
	$('a').click(function(event){
		isClicked=true;
	});
});

if(isClicked=false){
function showUnloading(evt){
//IE specific code to cancel the navigation
//if you put a text value in that property IE will try to confirm
//if you want to navigate off the page.
//The code also works with Firefox
	if(isClicked==false){
$('#modalWindow').show();

	evt.returnValue = "Are you sure you want to leave";

//Firefox GeckoDOM specific
//below is how to handle the cancelling of firefox
if (!alert('First message they will see'))
{
	
evt.preventDefault;
evt.stopPropagation; // this makes sure it does not bubble up
}

}
}
}

I use the Jquery to detect if it was a link that was clicked and disable any thing stopping it from moving to the next window. I also use JQuery to display the Modal Window which is a Div id in the body.

I know it’s ridicuously annoying but like I said, the target demographic doesn’t really know any better.

I use this sort of event to send a quick ajax snippet so I can time length of visits on the page. I do this even when moving between pages on the same site to get a feel for which pages on the site users are staying on longest (inferring that their content is probably most interesting). It’s good demographic data to silently collect.

It discourages me to see the objects abused by morons out to harass users though. A “Please don’t leave my site” message is harassment and nothing else. JordashTalon - I’d love to know the sites you’re putting this crap on so I can go ahead and blacklist them.

The other part that pisses me off to no end is inconsiderate and rude coders causing wide sections of Javascript functionality to be turned off by users because they’re fed up with the harassment - which in turn breaks legitimate use of that functionality.

It’s my first time trying it … if it works well it’s just what my clients are after and what they like, if it doesn’t work i’ll give up on the attempt. I’d never do this on an information website or other type of website. I agree with what you are saying.

Thankfully onbeforeunload is a proprietary event and only a couple of browsers will ever process that code. Even in those browsers you can kill it. So those with a knowledge of how the web works will never see that spam.

Anyway it will be extremely annoying for those who do have it enabled, don’t know how to kill it, and who press back or refresh.

Hey waddya know, I just got an email from a customer that didn’t like it at all… I should’ve listened to you guys sooner sorry about that.

At least I learned some Javascript in the process :blush:

It has equivalents in every major browser though (what events implemented where are some of the reasons jQuery and prototype are so popular).

which just means slightly more work to KILL THAT SPAM since you have to work out which event to kill.

If it ever becomes to common then browsers will implement a blocker the same way they did for popup window spam.

Wow - is that ever irritating! I have to agree with everyone else in this form - that looks like a great way to make sure a user NEVER comes back to your site. That’s marketing at its absolute worst.

heh heh I wouldn’t say it was that irritating, but close. :smiley:

But here’s the thing…if a visitor leaves, he is not interested, period, so he won’t come back anyway. So it doesn’t make any difference what he thinks of it.

This thread gives me the idea to do something like that. But instead, give them a free tip, free advice they can use. Something that is actually useful.

That may help a lot.

This website triggers the message when you refresh the page, which is pretty stupid.

You shouldn’t be embarassed about it. That’s what good web designers do, they test and then react based on the results.

wow cool suggestions really appreciate to read it

It’s good that you achieved it using jquery.

How is the jquery script able to distinguish between closing the browser, leaving for a different site, leaving for a different page on the same site, and refreshing the current page as JavaScript has no access to be able to tell which of those four is the reason why the current page wants to unload?

For my script this is irrelevant - I’m just making an ajax request to mark the end of the view of the page and stop the timer started when the server sent the page to begin with. Back to back page view sessions, like those caused by the user refreshing the page, are cleaned out when the admin goes to view the results.

I basically added a listener for every a tag in the page and each time an a tag is clicked I set the a variable to true, only if the variable was not true did I initiate the popup window notifying them that they were about to leave (which by the way I disabled).

My script I posted above should show the code

Oh and I never was able to figure out a way to get it to stop the popup if the user refreshed the page… Their may be a way.

No there isn’t because the same event is triggered regardless of the reason why the page is unloaded and for security reasons JavaScript has no access to know what will happen next (since it isn’t happening within the current page).

If all browers not support these code for detection then how google analytics and statcounter detects.

A user is defined as having left a site when their sessiont times out, not when they actually leave the page.

I think the default session time in Google Analytics is 30 mins but you can change it.

This means that if you spend longer than 30 mins on a page GA regards you as having left even though you haven’t really gone anywhere.

All server side checking involves the use of sessions (as only sessions are maintained between pages of a site). There are basically three ways of ending a session.

  1. There is a default amount of time after a session is created when that session will be automatically destroyed whether the person is or isn’t on the site any more.
  2. If you have people login when they arrive on your site then you can have the logout process destroy the session so that if they remember to logout you can more accurately record when they left.
  3. You can use a heartbeat ajax script in your web pages that basically calls a server side script at regular intervals to let the server side script know that the person is still on the site. Once the person leaves the site these regular calls to the server stop and when the server side script sees that the signals are no longer being sent it then knows that the person left the site within the last timeperiod (depending on how long between signals) - of course this method doesn’t work for those without ajax enabled in their browser.

this in turn allows you to record another demographic, which is % of users without AJAX support. Something that should be of interest to any site with any serious AJAX based functionality.

good !!I support you