Try the following
var clickHandler = function() {
// Your click handler
};
var anchors = document.getElementsByTagName("a");
for (var i = 0; i < anchors.length; i++) {
var current = anchors[i];
current.addEventListener('click', clickHandler, false);
}
Note: As Ӫ_._Ӫ pointed out this will not work on IE8 and lower as it doesn't support addEventListener
.
On IE8 you could use the following to subscribe to onclick
. It's not a perfect substitute as it requires everyone to be cooperative but it may be able to help you out
var subscribeToOnClick = function(element) {
if (element.onclick === undefined) {
element.onclick = clickHandler;
} else {
var saved = element.onclick;
element.onclick = function() {
saved.apply(this, arguments);
clickHandler.apply(this, arguments);
}
}
}
for (var i = 0; i < anchors.length; i++) {
var current = anchors[i];
subscribeToOnClick(current);
}