Event Handler in Object Works in IE not Firefox

Discussion in 'Javascript' started by drink.the.koolaid, Oct 4, 2007.

  1. 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>
    drink.the.koolaid, Oct 4, 2007
    #1
    1. Advertising

  2. 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


    --
    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
    Gregor Kofler, Oct 4, 2007
    #2
    1. Advertising

  3. >>>"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.
    drink.the.koolaid, Oct 4, 2007
    #3
  4. drink.the.koolaid

    David Mark Guest

    On Oct 4, 3:18 pm, "drink.the.koolaid" <>
    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.
    David Mark, Oct 4, 2007
    #4
  5. 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
    Thomas 'PointedEars' Lahn, Oct 4, 2007
    #5
  6. > On Oct 4, 5:49 pm, Thomas 'PointedEars' Lahn <> 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.
    drink.the.koolaid, Oct 11, 2007
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Juergen Theis
    Replies:
    0
    Views:
    634
    Juergen Theis
    Feb 21, 2006
  2. Replies:
    1
    Views:
    686
    Damien
    Feb 22, 2007
  3. Replies:
    0
    Views:
    158
  4. David McNerney
    Replies:
    4
    Views:
    163
    David McNerney
    Apr 7, 2006
  5. Replies:
    3
    Views:
    92
Loading...

Share This Page