Tracking Clicks on pages with messed up click handlers

done for nadahalli

by Aditya

HTML

<!doctype html>
<html>
<body>
    <ul><li><a href='crap' onclick="event.cancelBubble = true;if(event.stopPropagation) event.stopPropagation();location.href=this.href;return(false);">&#8226; Crap</a></li>
        <li><a href='crap' onclick="event.cancelBubble = true;if(event.stopPropagation) event.stopPropagation();location.href=this.href;return(false);">&#8226; Crap</a></li></ul>
</body>
</html>

JavaScript

(function(document){

function E(ev,h){function callback(e){e=e||event;h.apply(document,[e,e.target||e.srcElement]);}
if(document.addEventListener){document.addEventListener(ev,callback, false);} else if(document.attachEvent){document.attachEvent('on'+ev,callback);}return E;}

var node=null,links = document.getElementsByTagName("a");
E("mousedown",function(e,target){
  if(target.tagName.toLowerCase() !== "a"){return;}
  node = target;
})("mouseup",function(e,target){
  if(target.tagName.toLowerCase() === "a" && target === node){
    console.log(Array.prototype.indexOf.call(links,node));
  }
  node = null;
});
})(document);