Friday, February 23, 2024
 Popular · Latest · Hot · Upcoming
rated 0 times [  37] [ 2]  / answers: 1 / hits: 16667  / 15 Years ago, tue, march 10, 2009, 12:00:00

The following works fine on IE6, IE7, and chrome. Not working on ff 3.0.7.

<script src=prototype.js type=text/javascript ></script>
<script type=text/javascript>
Event.observe(window, 'load',
Event.observe(document.body, 'keydown', myEventHandler);
alert('window load');
function myEventHandler(evt) {
<body >
<input type=text /><br><br>

EDIT: By not working I mean myEventHandler is not firing in firefox.

EDIT2: Furthermore, it works fine when focus is on the input element. I want it fire for all keydowns.

More From » events


I have no idea why your code doesn't work, but it's overly complicated - this should do the trick:

document.observe('keydown', myEventHandler);

There's no need to wait for load as document is available immediately.

Your code doesn't work because not all key events originate within a document's body element. Opera has issues similar to the ones in Firefox, but the originating element seems to additionally depend on the placement of the mouse cursor.

Anyway, the fix is to just catch the events at document level, because as long as no one stops them from doing so, all DOM events will bubble up to document eventually.

[#99871] Monday, March 2, 2009, 15 Years  [reply] [flag answer]
Only authorized users can answer the question. Please sign in first, or register a free account.

Total Points: 676
Total Questions: 94
Total Answers: 81

Location: Thailand
Member since Thu, Apr 22, 2021
3 Years ago