Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   Javascript (http://www.velocityreviews.com/forums/f68-javascript.html)
-   -   Event Handler in Object Works in IE not Firefox (http://www.velocityreviews.com/forums/t933096-event-handler-in-object-works-in-ie-not-firefox.html)

drink.the.koolaid 10-04-2007 06:02 PM

Event Handler in Object Works in IE not Firefox
 
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>


Gregor Kofler 10-04-2007 06:16 PM

Re: Event Handler in Object Works in IE not Firefox
 
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...dEventListener


--
http://www.gregorkofler.at ::: Landschafts- und Reisefotografie
http://www.licht-blick.at ::: Forum für Multivisionsvorträge
http://www.image2d.com ::: Bildagentur für den alpinen Raum

drink.the.koolaid 10-04-2007 07:18 PM

Re: Event Handler in Object Works in IE not Firefox
 
>>>"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.


David Mark 10-04-2007 07:29 PM

Re: Event Handler in Object Works in IE not Firefox
 
On Oct 4, 3:18 pm, "drink.the.koolaid" <drink.the.kool...@gmail.com>
wrote:
> >>>"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.


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.







Thomas 'PointedEars' Lahn 10-04-2007 09:49 PM

Re: Event Handler in Object Works in IE not Firefox
 
drink.the.koolaid wrote:
>>>> "click". Is it really so hard to read the manuals (with the accompanying examples)?


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
--
realism: HTML 4.01 Strict
evangelism: XHTML 1.0 Strict
madness: XHTML 1.1 as application/xhtml+xml
-- Bjoern Hoehrmann

drink.the.koolaid 10-11-2007 01:28 PM

Re: Event Handler in Object Works in IE not Firefox
 
> On Oct 4, 5:49 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de> wrote:
> 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.



All times are GMT. The time now is 04:43 PM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.