Link tracking with Javascript help needed

Discussion in 'JavaScript' started by phantom, Feb 18, 2011.

  1. #1
    Hello,

    I am trying get some javascript code to track links that are marked as rel=external.

    The javascript will be in a tag on my site but so far I can get it it to work.

    Can anyone out there tell me what I am doing wrong from in the function below?

    function addEvent(elm, evType, fn, useCapture)
                        {
                          if (elm.addEventListener){
                              elm.addEventListener(evType, fn, useCapture);
                              return true;
                          } else if (elm.attachEvent){
                              var r = elm.attachEvent("on"+evType, fn);
                              return r;
                          } else {
                              alert("Handler could not be removed");
                          }
                        }
                        function externalLinks() {
                         if (!document.getElementsByTagName) return;
                         var anchors = document.getElementsByTagName("a");
                                     var newwindows =0;
                         for (var i=0; i<anchors.length; i++) {
                           var anchor = anchors[i].onmousedown = track;
                           if (anchor.getAttribute("href") && anchor.getAttribute("rel") == "external") {
                                    anchor.setAttribute("target","_blank");
                                                            //newwindows++;
                                            }
    
    function track() {
    preload_exit = new Image(0,0);
    preload_exit.src="http://trackersite.com/e.jpg?ref="+url;
    
    }
    
                         }
                        }
    
                        addEvent(window, "load", externalLinks);
    Code (markup):
     
    phantom, Feb 18, 2011 IP
  2. Cash Nebula

    Cash Nebula Peon

    Messages:
    1,197
    Likes Received:
    67
    Best Answers:
    0
    Trophy Points:
    0
    #2
    I think the problem is that addEvent requires four parameters but you have provided only three.
    addEvent(elm, evType, fn, useCapture)
    addEvent(window, "load", externalLinks);
     
    Cash Nebula, Feb 20, 2011 IP