Go Back   CodingForums.com > :: Client side development > JavaScript programming

Before you post, read our: Rules & Posting Guidelines

Reply
 
Thread Tools Rate Thread
Enjoy an ad free experience by logging in. Not a member yet? Register.
Old 09-12-2010, 08:59 PM   PM User | #1
sitNsmile
Regular Coder

 
sitNsmile's Avatar
 
Join Date: Dec 2009
Location: Charlotte, NC
Posts: 354
Thanks: 19
Thanked 2 Times in 2 Posts
sitNsmile is an unknown quantity at this point
onClick / tracking clicks - faster method?

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
Code:
<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
Code:
<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
sitNsmile is offline   Reply With Quote
Old 09-12-2010, 09:45 PM   PM User | #2
Old Pedant
Supreme Master coder!

 
Old Pedant's Avatar
 
Join Date: Feb 2009
Posts: 23,224
Thanks: 59
Thanked 3,996 Times in 3,965 Posts
Old Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to allOld Pedant is a name known to all
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:
Code:
            anchor.onclick = function () 
            {
                ajax = ajaxFunction();
                var url = base_url+ "app_trackClicks.php?url=" + this.href;
                ajax.open("GET",url,true);
                ajax.send(null);
            };
__________________
An optimist sees the glass as half full.
A pessimist sees the glass as half empty.
A realist drinks it no matter how much there is.
Old Pedant is offline   Reply With Quote
Reply

Bookmarks

Jump To Top of Thread


Thread Tools
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT +1. The time now is 11:03 PM.


Advertisement
Log in to turn off these ads.