I've failed to capture the onkeyup event in FireFox

J

Jake Barnes

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?
 
J

jhurstus

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;
}
 
A

Anthony Levensalor

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!
 
J

Jake Barnes

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

David Mark

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

Arnaud Diederen

Anthony Levensalor said:
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;
 
A

Anthony Levensalor

// 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!
 
T

Thomas 'PointedEars' Lahn

Anthony said:
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
 

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,982
Messages
2,570,185
Members
46,737
Latest member
Georgeengab

Latest Threads

Top