Event Handler in Object Works in IE not Firefox

  • Thread starter drink.the.koolaid
  • Start date
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>
 
G

Gregor Kofler

drink.the.koolaid meinte:
Hello all. I'm having trouble with Firefox (as I often do).

I wouldn't blame that on the (pretty standards compliant) browser.
Here is a simplified illustration of the problem. If you click the
link and get to google, then the code didn't work.
[snip]

<script type="text/javascript" language="javascript">
language-attribute is not needed.
function TestClass(){
var self=this;

this.addToPage=
function(){
var a=window.document.getElementById("lnkTest");
if(document.all){

Forget about document.all as "sniffer". Rather check for available event
methods.
alert("setting event handler for ie");
a.onclick=this.onLinkClick;
}else{
alert("setting event handler for non-ie");
a.addEventListener(click, self.onLinkClick, false);

"click". Is it really so hard to read the manuals (with the accompanying
examples)? [1]


Gregor

[1] http://developer.mozilla.org/en/docs/DOM:element.addEventListener
 
D

drink.the.koolaid

"click". Is it really so hard to read the manuals (with the accompanying examples)?
Thank you for the help. It would be nicer without the chastisement.
The example I was following also had the quotes... I just managed to
overlook them.

At the risk of further criticism:
Now that the event on the link fires, why does the page still navigate
to google despite returning false in the event handler? I didn't find
this information in your reference doc.
 
D

David Mark

Thank you for the help. It would be nicer without the chastisement.
The example I was following also had the quotes... I just managed to
overlook them.

At the risk of further criticism:
Now that the event on the link fires, why does the page still navigate
to google despite returning false in the event handler? I didn't find
this information in your reference doc.

Did you read the part about the preventDefault method?


this.onLinkClick=
function(e){
alert("It Worked!");
if (e && e.preventDefault) { e.preventDefault(); }
return false;
}
}

Also, you should use attachEvent for IE or just use DOM0 for
everything.
 
T

Thomas 'PointedEars' Lahn

Please learn to quote. http://jibbering.com/faq/
Thank you for the help. It would be nicer without the chastisement.

When people are doing something really stupid, they should be told so.
Thus they will hopefully not repeat that mistake.
The example I was following also had the quotes... I just managed to
overlook them.

It seems strange to assume that copy and paste went out of fashion, given
the number of copy-and-pray examples posted here.
At the risk of further criticism:
Now that the event on the link fires, why does the page still navigate
to google despite returning false in the event handler? I didn't find
this information in your reference doc.

Because you have not prevented the default action. RTFM.


PointedEars
 
D

drink.the.koolaid

Because you have not prevented the default action. RTFM.

Exactly which FM and which part of said FM is not always apparent,
especially to one who's new to Firefox. I did read the FAQ and
Googled for an answer before posting. Besides, the "addEventListener"
topic in the FM Gregor mentioned (http://developer.mozilla.org/en/docs/
DOM:element.addEventListener) oddly makes no mention of
"preventDefault". How would I know what to look for without asking?

Despite the snide presentation, I do appreciate the info. So, thank
you Pointy Ears / Gregor.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,567
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top