Click to See Complete Forum and Search --> : Telling when a user has left your website


theMitch
01-16-2003, 10:40 PM
Hi

I would like to know when a user has left my website. Is there a way of doing this in Javascript or HTML (or PHP even)?

At first I thought myabe onUnload might work, but that is invoked web the page is changed and in some cases the user may be going to another page on my website and therefore not really leaving the website.

Does this make sense? I want to know when they completely leave the website (or close the browser) and not just move to another one of my pages.

Thanks in advance

Martin

pyro
01-16-2003, 10:46 PM
One possible solution would be to have an onUnload, but disable it if they stay in your URL. The best way to do this would probably be to get the URL with top.location.href, split it to only take http://www.thedomain.com and then match it up to see if that equals http://www.yourdomain.com

theMitch
01-16-2003, 10:58 PM
Hi Pyro

Thanks for the prompt reply. That sounds like the sort of thing I'm looking for, however, I have to admit I am a real newbie to Javascript so am not sure how I would code that?

Currently my code has the following:

<body onUnload="javascript:void winpopup('leaving.php',200,300,'no')"


Then, in my leaving.php I was going to ask like an exit poll - but this is on relevant if they leave the actual site, not simply change page.

Where would I put the javascript code to check the href of the page moved to?

Thanks again.

Martin

pyro
01-16-2003, 11:22 PM
This should work. Add to all of your pages.

<html>
<head>
<script type="text/javascript">

function openpopup()
{
pageURL = top.location.href;
holder = pageURL.split("/");

ending = "";
for (i=0;i < 3; i++)
{
ending += holder[i] += "/";
}

if (ending == "http://www.yourdomain.com/")
{
}
else
{
var url = "leaving.php";
var name = "popup";
var windowproperties = "width=200,height=300,left=150,top=150,location=no,toolbar=no,menubar=no,scrollbars=no,resizable=no";
window.open(url, name, windowproperties);
}
}

</script>
</head>
<body onUnload="openpopup();">
</body>
</html>NOTE: the var windowproperties line all goes on one line... Also, change the item in bold to equal yourdomain. :)

theMitch
01-16-2003, 11:48 PM
Hi Pyro

Thanks for the code. I have implemented it but found it didn't have the desired effect.

To see why, I added a couple of Alerts() to see what was held in the variables holder and ending.

When I do an alert(holder) after the line holder = pageURL.split("/"); I get the following results "http:,,www.mydomain.com,page1.html" but the trouble is I clicked on to www.google.com from page1.html (ie I am leaving myDomain.com), so I would have expected it to say "http:,,www.google.com".

Is this how you expected it to work?

Thanks

Martin

pyro
01-17-2003, 12:17 AM
hmm...I'll have to look into it. It works from my hdd, so I'll have to wait until I have time to upload it and see if it works differently online...Sorry 'bout that, I'll try to figure it out.

pyro
01-17-2003, 07:21 AM
Well, I'm not sure what the solution for this is. I think the problem is that even if you are going to a different domain, you are still on your domain when the function runs, so it never pops up the window. hmm....