/    Sign up×
Community /Pin to ProfileBookmark

Telling when a user has left your website

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

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@pyroJan 17.2003 — 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
Copy linkTweet thisAlerts:
@theMitchauthorJan 17.2003 — 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
Copy linkTweet thisAlerts:
@pyroJan 17.2003 — This should work. Add to all of your pages.

&lt;html&gt;
&lt;head&gt;
&lt;script type="text/javascript"&gt;

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

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

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

&lt;/script&gt;
&lt;/head&gt;
&lt;body onUnload="openpopup();"&gt;
&lt;/body&gt;
&lt;/html&gt;
NOTE: the var windowproperties line all goes on one line... Also, change the item in bold to equal yourdomain. ?
Copy linkTweet thisAlerts:
@theMitchauthorJan 17.2003 — 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 [FONT=courier new]holder = pageURL.split("/");[/FONT] 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
Copy linkTweet thisAlerts:
@pyroJan 17.2003 — 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.
Copy linkTweet thisAlerts:
@pyroJan 17.2003 — 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....
×

Success!

Help @theMitch spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.28,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...