I've failed to capture the onkeyup event in FireFox

Discussion in 'Javascript' started by Jake Barnes, Jan 10, 2008.

  1. Jake Barnes

    Jake Barnes Guest

    Please go here:

    http://www.cyberbitten.com/groups.php

    You can login with this info:

    user: test
    password: test


    I'm on a Linux machine today, so I haven't been able to test this page
    using Internet Explorer, but my co-worker tested this page with IE and
    says it is working. So the problem is specific to FireFox.

    We are using Simon Wilison's function addLoadEvent.
    http://simonwillison.net/2004/May/26/addLoadEvent/

    What we'd like to do is capture use of the Enter key. First, we use
    addLoadEvent to attach the detectEnterKey function to the textarea
    called "chat_input":


    addLoadEvent(function() {
    if (document.getElementById("chat_input")) {
    var referenceToChatInputButton =
    document.getElementById("chat_input");
    referenceToChatInputButton.onkeyup = function() {
    detectEnterKey();
    return false;
    }
    }
    });


    This is one version of detectEnterKey. For some reason, in FireFox, I
    have not been able to get the alert() to function:

    function detectEnterKey(e) {
    if(document.getElementById("chat_input")) {
    var referenceToChatInputButton =
    document.getElementById("chat_input");
    if (referenceToChatInputButton.onkeyup) {
    if (window.event) {
    keycode = window.event.keyCode;
    } else if (e) {
    keycode = e.which;
    }
    alert(keycode);
    if (keycode == 13) {
    postNewChatMessage();
    void(0);
    }
    }
    }
    }



    Here is another version of the function:

    // 1-10-08 - RK - Captures Enter Key Stroke and initiates chat_send
    (Talk) button
    function detectEnterKey(e) {
    if(document.getElementById("chat_input")) {
    var referenceToChatInputButton =
    document.getElementById("chat_input");
    if (referenceToChatInputButton.onkeyup) {
    var KeyID = (window.event) ? event.keyCode : e.which;
    alert(KeyID);
    if (window.event) {
    if(event.keyCode == 13) {
    postNewChatMessage();
    }
    }
    }
    }
    }



    Again, these capture the Enter key in IE, but not FireFox. What are we
    missing?
    Jake Barnes, Jan 10, 2008
    #1
    1. Advertising

  2. Jake Barnes

    Guest

    On Jan 10, 1:08 pm, Jake Barnes <> wrote:
    > Please go here:
    >
    > http://www.cyberbitten.com/groups.php
    >
    > You can login with this info:
    >
    > user: test
    > password: test
    >
    > I'm on a Linux machine today, so I haven't been able to test this page
    > using Internet Explorer, but my co-worker tested this page with IE and
    > says it is working. So the problem is specific to FireFox.
    >
    > We are using Simon Wilison's function addLoadEvent.http://simonwillison.net/2004/May/26/addLoadEvent/
    >
    > What we'd like to do is capture use of the Enter key. First, we use
    > addLoadEvent to attach the detectEnterKey function to the textarea
    > called "chat_input":
    >
    > addLoadEvent(function() {
    >         if (document.getElementById("chat_input")) {
    >                 var referenceToChatInputButton =
    > document.getElementById("chat_input");
    >                 referenceToChatInputButton.onkeyup = function() {
    >                         detectEnterKey();
    >                         return false;
    >                 }
    >         }
    >
    > });
    >
    > This is one version of detectEnterKey. For some reason, in FireFox, I
    > have not been able to get the alert() to function:
    >
    > function detectEnterKey(e) {
    >         if(document.getElementById("chat_input")) {
    >                 var referenceToChatInputButton =
    > document.getElementById("chat_input");
    >                 if (referenceToChatInputButton.onkeyup) {
    >                         if (window.event) {
    >                                 keycode = window.event.keyCode;
    >                         } else if (e) {
    >                                 keycode = e.which;
    >                         }
    > alert(keycode);
    >                         if (keycode == 13) {
    >                                 postNewChatMessage();
    >                                 void(0);
    >                         }
    >                 }
    >         }
    >
    > }
    >
    > Here is another version of the function:
    >
    > // 1-10-08 - RK -  Captures Enter Key Stroke and initiates chat_send
    > (Talk) button
    > function detectEnterKey(e) {
    >         if(document.getElementById("chat_input")) {
    >                 var referenceToChatInputButton =
    > document.getElementById("chat_input");
    >                 if (referenceToChatInputButton.onkeyup) {
    >                         var KeyID = (window.event) ? event.keyCode : e.which;
    >                         alert(KeyID);
    >                         if (window.event) {
    >                                 if(event.keyCode == 13) {
    >                                         postNewChatMessage();
    >                                 }
    >                         }
    >                 }
    >         }
    >
    > }
    >
    > Again, these capture the Enter key in IE, but not FireFox. What are we
    > missing?


    Looks like the event object is not being passed on to deleteEnterKey.
    In IE, the event object is accessed via the global window.event, but
    in FF the event object is passed directly to the event handler.

    In the anonymous function assigned to
    referenceToChatInputButton.onkeyup, you'll need to add a parameter "e"
    and then pass this to deleteEnterKey.


    referenceToChatInputButton.onkeyup = function(e) {
    detectEnterKey(e);
    return false;
    }
    , Jan 10, 2008
    #2
    1. Advertising

  3. On 1/10/2008 5:18 PM, wrote:
    >
    > referenceToChatInputButton.onkeyup = function(e) {
    > detectEnterKey(e);
    > return false;
    > }



    Declaring the function with e is plenty, your onkeyup assignment ought
    to be:

    someElement.onkeyup = somefunction

    function somefunction (e) {
    ....

    }


    All the best,
    ~A!


    --
    anthony at my pet programmer dot com
    Anthony Levensalor, Jan 10, 2008
    #3
  4. Jake Barnes

    Jake Barnes Guest

    On Jan 10, 5:18 pm, "" <> wrote:
    > On Jan 10, 1:08 pm, Jake Barnes <> wrote:
    >
    >
    >
    > > Please go here:

    >
    > >http://www.cyberbitten.com/groups.php

    >
    > > You can login with this info:

    >
    > > user: test
    > > password: test

    >
    > > I'm on a Linux machine today, so I haven't been able to test this page
    > > using Internet Explorer, but my co-worker tested this page with IE and
    > > says it is working. So the problem is specific to FireFox.

    >
    > > We are using Simon Wilison's function addLoadEvent.http://simonwillison.net/2004/May/26/addLoadEvent/

    >
    > > What we'd like to do is capture use of the Enter key. First, we use
    > > addLoadEvent to attach the detectEnterKey function to the textarea
    > > called "chat_input":

    >
    > > addLoadEvent(function() {
    > > if (document.getElementById("chat_input")) {
    > > var referenceToChatInputButton =
    > > document.getElementById("chat_input");
    > > referenceToChatInputButton.onkeyup = function() {
    > > detectEnterKey();
    > > return false;
    > > }
    > > }

    >
    > > });

    >
    > > This is one version of detectEnterKey. For some reason, in FireFox, I
    > > have not been able to get the alert() to function:

    >
    > > function detectEnterKey(e) {
    > > if(document.getElementById("chat_input")) {
    > > var referenceToChatInputButton =
    > > document.getElementById("chat_input");
    > > if (referenceToChatInputButton.onkeyup) {
    > > if (window.event) {
    > > keycode = window.event.keyCode;
    > > } else if (e) {
    > > keycode = e.which;
    > > }
    > > alert(keycode);
    > > if (keycode == 13) {
    > > postNewChatMessage();
    > > void(0);
    > > }
    > > }
    > > }

    >
    > > }

    >
    > > Here is another version of the function:

    >
    > > // 1-10-08 - RK - Captures Enter Key Stroke and initiates chat_send
    > > (Talk) button
    > > function detectEnterKey(e) {
    > > if(document.getElementById("chat_input")) {
    > > var referenceToChatInputButton =
    > > document.getElementById("chat_input");
    > > if (referenceToChatInputButton.onkeyup) {
    > > var KeyID = (window.event) ? event.keyCode : e.which;
    > > alert(KeyID);
    > > if (window.event) {
    > > if(event.keyCode == 13) {
    > > postNewChatMessage();
    > > }
    > > }
    > > }
    > > }

    >
    > > }

    >
    > > Again, these capture the Enter key in IE, but not FireFox. What are we
    > > missing?

    >
    > Looks like the event object is not being passed on to deleteEnterKey.
    > In IE, the event object is accessed via the global window.event, but
    > in FF the event object is passed directly to the event handler.
    >
    > In the anonymous function assigned to
    > referenceToChatInputButton.onkeyup, you'll need to add a parameter "e"
    > and then pass this to deleteEnterKey.
    >
    > referenceToChatInputButton.onkeyup = function(e) {
    > detectEnterKey(e);
    > return false;
    >
    > }




    Thanks much. This was perfect.
    Jake Barnes, Jan 10, 2008
    #4
  5. Jake Barnes

    David Mark Guest

    On Jan 10, 4:08 pm, Jake Barnes <> wrote:
    > Please go here:
    >
    > http://www.cyberbitten.com/groups.php
    >
    > You can login with this info:
    >
    > user: test
    > password: test
    >
    > I'm on a Linux machine today, so I haven't been able to test this page
    > using Internet Explorer, but my co-worker tested this page with IE and
    > says it is working. So the problem is specific to FireFox.
    >
    > We are using Simon Wilison's function addLoadEvent.http://simonwillison.net/2004/May/26/addLoadEvent/
    >
    > What we'd like to do is capture use of the Enter key. First, we use
    > addLoadEvent to attach the detectEnterKey function to the textarea
    > called "chat_input":
    >
    > addLoadEvent(function() {
    >         if (document.getElementById("chat_input")) {
    >                 var referenceToChatInputButton =
    > document.getElementById("chat_input");


    Is this really a button? If so, you are going about this the wrong
    way (you should attach the event to the text input.)

    >                 referenceToChatInputButton.onkeyup = function() {
    >                         detectEnterKey();
    >                         return false;
    >                 }


    Clearly that won't work. You didn't pass the event.

    >         }
    >
    > });
    >
    > This is one version of detectEnterKey. For some reason, in FireFox, I
    > have not been able to get the alert() to function:
    >
    > function detectEnterKey(e) {
    >         if(document.getElementById("chat_input")) {
    >                 var referenceToChatInputButton =
    > document.getElementById("chat_input");
    >                 if (referenceToChatInputButton.onkeyup) {


    What is that supposed to do?

    >                         if (window.event) {
    >                                 keycode = window.event.keyCode;
    >                         } else if (e) {
    >                                 keycode = e.which;
    >                         }


    Bad inference.

    > alert(keycode);
    >                         if (keycode == 13) {
    >                                 postNewChatMessage();
    >                                 void(0);


    What is the point of the void call?

    >                         }
    >                 }
    >         }
    >
    > }
    >
    > Here is another version of the function:
    >
    > // 1-10-08 - RK -  Captures Enter Key Stroke and initiates chat_send
    > (Talk) button
    > function detectEnterKey(e) {
    >         if(document.getElementById("chat_input")) {
    >                 var referenceToChatInputButton =
    > document.getElementById("chat_input");
    >                 if (referenceToChatInputButton.onkeyup) {
    >                         var KeyID = (window.event) ? event.keyCode : e.which;


    Same bad inference.

    [snip]

    >
    > Again, these capture the Enter key in IE, but not FireFox. What are we
    > missing?


    (Assuming a button "chat_input" and a text input "chat_text")
    Put this in your load event:

    myButton = document.getElementById('chat_input');
    myTextInput = document.getElementById('chat_text');

    if (myTextInput && myButton) {
    myTextInput.onkeyup = function(e) {
    e = e || window.event;
    if ((e.which || e.keyCode) == 13) {
    postNewChatMessage();
    return false;
    }
    };

    myButton.onclick = postNewChatMessage;
    }

    Also, you should detect getEBI before calling it.
    David Mark, Jan 11, 2008
    #5
  6. Anthony Levensalor <> writes:

    > On 1/10/2008 5:18 PM, wrote:
    > > referenceToChatInputButton.onkeyup = function(e) {
    > > detectEnterKey(e);
    > > return false;
    > > }

    >
    >
    > Declaring the function with e is plenty, your onkeyup assignment ought
    > to be:
    >
    > someElement.onkeyup = somefunction
    >
    > function somefunction (e) {


    // If I'm not mistaken, IE doesn't pass the event as parameter, put
    // puts it in as window.event. Therefore, I'd add (untested):

    e = e || window.event;

    > ....
    >
    > }
    >
    >
    > All the best,
    > ~A!
    >
    >
    > --
    > anthony at my pet programmer dot com
    Arnaud Diederen, Jan 11, 2008
    #6
  7. On 1/11/2008 4:25 AM, Arnaud Diederen (aundro) wrote:

    >> function somefunction (e) {

    >
    > // If I'm not mistaken, IE doesn't pass the event as parameter, put
    > // puts it in as window.event. Therefore, I'd add (untested):
    >
    > e = e || window.event;


    Absolutely. I didn't include anything in the body, was just showing th
    structure, but that is, in my experience, 100% correct.

    All the best,
    ~A!


    --
    anthony at my pet programmer dot com
    Anthony Levensalor, Jan 11, 2008
    #7
  8. Anthony Levensalor wrote:
    > On 1/10/2008 5:18 PM, wrote:
    >> referenceToChatInputButton.onkeyup = function(e) {
    >> detectEnterKey(e);
    >> return false;
    >> }

    >
    > Declaring the function with e is plenty, your onkeyup assignment ought
    > to be:
    >
    > someElement.onkeyup = somefunction
    >
    > function somefunction (e) {
    > ....
    >
    > }


    Your code is semantically equal to the original code, it is only marginally
    more compatible[1], and spoils the namespace needlessly.

    [1] http://PointedEars.de/es-matrix


    PointedEars
    Thomas 'PointedEars' Lahn, Jan 11, 2008
    #8
    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. Brad
    Replies:
    3
    Views:
    15,669
  2. Matthew
    Replies:
    5
    Views:
    123
    Lasse Reichstein Nielsen
    Feb 10, 2004
  3. Trent

    Help Capturing an onkeyup event

    Trent, Apr 23, 2004, in forum: Javascript
    Replies:
    3
    Views:
    159
    Evan Wong
    May 26, 2004
  4. Evan Wong

    OnKeyUp event does not work

    Evan Wong, May 26, 2004, in forum: Javascript
    Replies:
    2
    Views:
    227
    Evan Wong
    May 27, 2004
  5. Cartoper

    HOWTO: capture onKeyUp on <body>

    Cartoper, Oct 30, 2007, in forum: Javascript
    Replies:
    1
    Views:
    80
    Bart Van der Donck
    Oct 30, 2007
Loading...

Share This Page