Forum Moderators: open
I was looking at Google, and noticed that they use the following for links from their search results.
Script:
<script>
<!--
function clk(el,ct,cd) {if(document.images){(new Image()).src="/url?sa=T&ct="+escape(ct)+"&cd="+escape(cd)+"&url="+escape(el.href);}return true;}
//-->
</script>
The Link:
<a href=http://www.domain.com/ onmousedown="return clk(this,'res',123)">Domain is Here</a>
I don't know much javascript. Can someone let me know what the purpose/function of this is?
The reason I ask is that I would like to link out in a similar manner and track the clicks that go out. I'm hoping that this Google style of linking might provide this option.
In other words, the search engine spiders can follow the link out, but that I could modify the parameters so that I could have an unique number value for the link, and when the user physically clicked on the link (onmousedown), then it would take the unique ID number for the link and have it actually go through a cgi program that can record the clickthrough for me with something like:
domain.com/bounce.cgi?i=1
Isn't Google essentially doing something like this form themselves with the above code or (maybe more likely) am I way off course here?
What I don't see is how this works reliably. Does the browser always have time to make requests for images, when <1ms later it's being directed off to a new URL? It appears that it does. In which case, if you can handle the cgi, then the JS should be quite easy...
function clk(link,i)
{
var pathToCGI = "domain.com/bounce.cgi"
if(document.images)
{
(new Image()).src=pathToCGI+"?i="+i;
// (new Image()).src=pathToCGI+"?i="+i+"&url="+escape(link.href)
}
return true;
} <a href="http://www.domain.com/" onmousedown="return clk(this,123)">Domain is Here</a>
This is the simple version that just sends some id number.
To get the clicked URL, without having to hard code it into the function call, you can get it via the link's href property. This is why I have also included the keyword, this, in the function call.
Does it work?
I would guess that search engine spiders like googlebot, yahoo, etc. will be able to follow the link without problem without generating a click through the cgi script, unless somehow the onmousedown="return clk(this,123)" within the <a href ....> tag would cause a problem for them. I'm not sure...
Thanks again.