D
drink.the.koolaid
Hello all. I'm having trouble with Firefox (as I often do). I have a
javascript object that has an addToPage method to hook event handlers
to the links on the current page. Setting the onclick property of the
links works in IE but does not in Firefox. I've added
addEventListener code but this only seems to work if calling a
function that is not an object's method. Any ideas how to make this
work? One alternative is to ditch the javascript class alltogether
and use standard functions but I strongly think this is a tradeoff
that I shouldn't have to make.
Here is a simplified illustration of the problem. If you click the
link and get to google, then the code didn't work.
<html>
<body>
<a id="lnkTest" href="http://www.google.com">My Test Link</a>
<script type="text/javascript" language="javascript">
function TestClass(){
var self=this;
this.addToPage=
function(){
var a=window.document.getElementById("lnkTest");
if(document.all){
alert("setting event handler for ie");
a.onclick=this.onLinkClick;
}else{
alert("setting event handler for non-ie");
a.addEventListener(click, self.onLinkClick, false);
}
}
this.onLinkClick=
function(){
alert("It Worked!");
return false;
}
}
var test = new TestClass();
test.addToPage();
</script>
</body>
</html>
javascript object that has an addToPage method to hook event handlers
to the links on the current page. Setting the onclick property of the
links works in IE but does not in Firefox. I've added
addEventListener code but this only seems to work if calling a
function that is not an object's method. Any ideas how to make this
work? One alternative is to ditch the javascript class alltogether
and use standard functions but I strongly think this is a tradeoff
that I shouldn't have to make.
Here is a simplified illustration of the problem. If you click the
link and get to google, then the code didn't work.
<html>
<body>
<a id="lnkTest" href="http://www.google.com">My Test Link</a>
<script type="text/javascript" language="javascript">
function TestClass(){
var self=this;
this.addToPage=
function(){
var a=window.document.getElementById("lnkTest");
if(document.all){
alert("setting event handler for ie");
a.onclick=this.onLinkClick;
}else{
alert("setting event handler for non-ie");
a.addEventListener(click, self.onLinkClick, false);
}
}
this.onLinkClick=
function(){
alert("It Worked!");
return false;
}
}
var test = new TestClass();
test.addToPage();
</script>
</body>
</html>