Open or hit tracking page when link clicked

B

Ben Amada

Hi ...

I have an HTML page containing a bunch of <a> links. Some of the links
redirect the visitor to a page at a different website in a new browser
window (target=_blank) and other links are "mailto" links.

What I want to do is record a little information when they click on a link
(mainly which link they are clicking on). I can put together a server side
webpage that can receive this information and store it in a DB. I'm just
not sure what JavaScript function I would call to "hit" that page. The page
would not be visible. I'm fairly certain AJAX or an iframe would work --
even though I have limited experience with these two techniques. Here's an
example of what I'm looking to do:

<a href="http://www.example.com/"
target="_blank"
onclick="doTracker('example.com');">
Visit this Site
</a>

<a href="mailto:[email protected]"
onclick="doTracker('(e-mail address removed)');">
Email someone
</a>

The doTracker() function would just do two things

1. somehow hit my tracking page (maybe using querystring variables).
2. return true at the end so the link will be executed.

I can pass the information on the link the visitor clicked on via
querystring variables (this seems simplest to do).

My question is, how can I open or hit my tracking page "behind the scenes"?
AJAX, iframe, or something else?

Thanks in advance,
Ben
 
B

Ben Amada

Randy said:
Ben Amada said the following on 3/11/2007 1:44 PM:

Something very very trivially simple.

How do I run a server side Script?
<URL: http://jibbering.com/faq/index.html#FAQ4_34>

onclick="return hitTheServer(this.href)"

function hitTheServer(parameter){
var dummyImage = new Image();
dummyImage.src = "scriptURL.php?param=" + parameter;
return true;
}

The server script would then read the param and act on it accordingly.

Hi Randy,

I knew it had to be something simple. I'm kicking myself now since I was
just looking something else up in the FAQ prior to posting my question and
didn't even bother to check for something on this topic.

BTW, the "this.href" will definitely make doing this a little easier.

Thank you so much,
Ben
 
E

Evertjan.

Randy Webb wrote on 11 mrt 2007 in comp.lang.javascript:
How do I run a server side Script?
<URL: http://jibbering.com/faq/index.html#FAQ4_34>

onclick="return hitTheServer(this.href)"

function hitTheServer(parameter){
var dummyImage = new Image();
dummyImage.src = "scriptURL.php?param=" + parameter;
return true;
}

Two Q, since I like compact code:

Is it useful to externalize
var dummyImage = new Image();
using only one new Image() for the whole page?

Do I need the return true?
[not setting return false surely is enough?]

========================================

<script type='text/javascript'>
var dum = new Image();
function doit(x){ dum.src='/doit/?p='+x };
</script>

<a href="http://blah.bla/" onclick="doit(this.href)">
doit</a>
 
A

ASM

Ben Amada a écrit :
Hi ...

I have an HTML page containing a bunch of <a> links. Some of the links
redirect the visitor to a page at a different website in a new browser
window (target=_blank) and other links are "mailto" links.

What I want to do is record a little information when they click on a link
(mainly which link they are clicking on). I can put together a server side
webpage that can receive this information and store it in a DB. I'm just
not sure what JavaScript function I would call to "hit" that page.

why not to use server side functions ?

<a href="http://www.example.com/"
target="_blank"
onclick="doTracker(this);">
Visit this Site
</a>

<a href="mailto:[email protected]"
onclick="doTracker(this);">
Email someone
</a>


function doTracker(what) {
self.location='tracking.php?link='+encodeURI(what.href);
}

tracking.php
analyzes link clicked (the what href)
then sends back the file already displayed (that's to say itself)

during this time the link does its normal html job
My question is, how can I open or hit my tracking page "behind the scenes"?
AJAX, iframe, or something else?

in same window ... no ?
 
E

Evertjan.

Randy Webb wrote on 11 mrt 2007 in comp.lang.javascript:
Truly dependable would be a server app that redirected to the
requested URL:

<a
href="http://www.yourServer.com/redirection.php?param=http://www.google
.com">
Go to Google
</a>

And then have the server do a redirect after logging the click.

That's whay I am doing for years now, I hoped for a simpler way,
since you have to escape "&" in a second querystring:

href="/redirection.php?param=http://www.google.com/?a=1&b=3">

[this will leave the b=3 at the redirection]
 
R

Richard Cornford

ASM wrote:
function doTracker(what) {
self.location='tracking.php?link='+encodeURI(what.href);
}
<snip>

The - encodeURI - would be dangerous to use in this context, as its
subject here is a URL. The appropriate method for use in this context
is - encodeURIComponent -. (See ECMA 262 3rd Ed. Section 15.1.3).

Richard.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,900
Latest member
Nell636132

Latest Threads

Top