Here is a script that looks at the links that exist on a page and binds a mousedown
event listener to each link. When a mousedown
event is triggered it calls a function that creates an alert with the link's href. In IE9+, this works fine, but in IE8, this is undefined.
<html>
<body>
<a href="http://www.example.com">test</a>
<script>
var c=function(){alert(this.href)};
var a=document.getElementsByTagName("a");
for(var b=0; b<a.length; b++) {
if (a[b].addEventListener) {
a[b].addEventListener("mousedown",c,false);
} else {
a[b].attachEvent("onmousedown",c);
}
}
</script>
</body>
</html>
I've tried adding this
and this.href
as arguments to the function, but it doesn't look like those functions accept arguments. Anyone know how I would get this to work?