Forum Moderators: open
<script>
function track(link)
{
new Image().src = 'http://myserverurl.com/tracking/track_click1.php?id=1&href='+ location.href;
}
</script>
<a href="http://www.linktobetracked.com" onclick="track(this)">Link</a>
Can somebody provide any kind of hint?
Thanks
yd0802:
The problem is that FF has not the time to load the image before loading the page pointed by the <a> link.
alternative solutions:
- link to a redirect page on your domain: /redir.php?u=http://www.linktobetracked.com
- pause a quarter, or half a second before exiting from track()
I cannot use redirect because I am using a free host and that is against the TOS.
However, I found the solution. As you mentioned, it appears indeed that FF has not the time to load the image. However, adding target="_blank" in the <a> gives a little extra time ... just enough for the onclick to be successfull.
Although it opens a new windows for the link (no so elegant), it does solve my problem.
Thanks for all support.
Here is a working solution:
<script>
function track(link)
{
new Image().src = 'http://myserverurl.com/tracking/track_click1.php?id=1&href='+ location.href;if (link.target && (link.target!="_top")&&(link.target!="_self")&&(link.target!="_parent")) return true;
setTimeout(function(){location=link.href},0)
return false;
}
</script>
<a href="http://www.linktobetracked.com" onclick="return track(this);" target="_parent">Link</a>
What does it do ?
If there is a "target" that opens a new window (either target="_blank", or a named window target="A_name"), the script allows the normal "click on a link" processus to proceed.
If there is no target, or one that doesn't open a new window, il stops the normal clicking processus by returning "false". It sets a timer to do this instead.
Note: waiting for "0" second means that the timeout will start immediately after the current script as ended. 0 is enough to allow FF to request the image. Even if it has not the time to complete the load, your sever log will show the request.
I've not refined the code to open the link correctly when "target" is either "_top" and "_parent".
[edited by: Achernar at 2:07 pm (utc) on Feb. 22, 2008]