Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 2 of 2
  1. #1
    Regular Coder sitNsmile's Avatar
    Join Date
    Dec 2009
    Location
    Charlotte, NC
    Posts
    358
    Thanks
    19
    Thanked 2 Times in 2 Posts

    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

  • #2
    Supreme Master coder! Old Pedant's Avatar
    Join Date
    Feb 2009
    Posts
    25,032
    Thanks
    75
    Thanked 4,325 Times in 4,291 Posts
    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.


  •  

    Posting Permissions

    • You may not post new threads
    • You may not post replies
    • You may not post attachments
    • You may not edit your posts
    •