Forum Moderators: open

Message Too Old, No Replies

What is this Script Doing

         

rover

8:10 pm on Jul 2, 2004 (gmt 0)

10+ Year Member



Hi-

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?

Bernard Marx

9:05 pm on Jul 2, 2004 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



I'm tempted to think that they are tracking onward clicks by creating an image that has all the required information in its src path. This either goes to their cgi bin, or they simply run over their server logs for those requests (that last one, though, I guess would take a fair amount of time).

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?

rover

9:56 pm on Jul 2, 2004 (gmt 0)

10+ Year Member



Wow, that was fast. I just tried that and it worked like a charm for the few clicks that I just tried. It linked through fine to the URL, and it also recorded the click through bounce.cgi. Thanks very much!

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.