...

View Full Version : onClick / tracking clicks - faster method?



sitNsmile
09-12-2010, 08:59 PM
Okay, so I built up 2 codes, which do the same cause. but which one would be most efficient? or maybe which one would be best compatible for browsers?

CODE 1 :

- this code allows for me to use an #id


<a onclick="trackClick('1');" href="http://www.google.com" target="_new">(track 1)</a>

function trackClick(trackid) {
ajax = ajaxFunction();
var url = base_url+ "app_trackClicks.php";
url = url+ "?trackid=" +trackid;
ajax.open("GET",url,true);
ajax.send(null);
}



CODE 2
- using the REL


<a rel="randomclicks" href="http://www.google.com" target="_new">(track 2)</a>

function addLoadEvent(func)
{
var oldonload = window.onload;
if (typeof window.onload != 'function'){
window.onload = func;
} else {
window.onload = function(){
oldonload();
func();
}
}

}

addLoadEvent(randomclicks);

function randomclicks()
{
if (!document.getElementsByTagName){ return; }
var anchors = document.getElementsByTagName("a");

// loop through all anchor tags
for (var i=0; i<anchors.length; i++){
var anchor = anchors[i];

if ((anchor.getAttribute("rel") == "randomclicks")){
anchor.onclick = function ()
{
ajax = ajaxFunction();
var url = base_url+ "app_trackClicks.php";
url = url+ "?randomclicks";
ajax.open("GET",url,true);
ajax.send(null);
};
}
}
}


Thanks

Old Pedant
09-12-2010, 09:45 PM
Well, clearly it will take a few more microseconds--possibly milliseconds--to do the setup the second way. After all, it has to loop through all then anchor tags, find the rel attribute, etc., etc.

But who cares? That only happens at page load time. Who will ever see it happen?

The AJAX code is identical and will run at exactly the same speed, when the click actually occurs. (And in both cases should be an invisible amount of time.)

Of course, your second set of code is kind of useless for actually tracking clicks. As it makes no differentiation between *WHICH* <a> was clicked on. Wouldn't it be better to do something like:


anchor.onclick = function ()
{
ajax = ajaxFunction();
var url = base_url+ "app_trackClicks.php?url=" + this.href;
ajax.open("GET",url,true);
ajax.send(null);
};



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum