Prototype / javascript - firefox does not fire the / keydown key event if focus is in the text box

In IE6, IE7 and chrome works fine. Does not work on ff 3.0.7.

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

EDIT: “Not working” I mean that myEventHandler does not fire in firefox.

EDIT2: Also, it works great when the focus is on the input element. I want it to work on all keystrokes.

+3
source share
4 answers

I have no idea why your code is not working, but it's too complicated - this should do the trick:

document.observe('keydown', myEventHandler);

There is no need to wait load, because it documentis available immediately.


, . Opera , , Firefox, , -, .

, , document, , , DOM document.

+7

, keydown Firefox . , Safari Firefox, IE. , "", "document.body", , :

document.observe('dom:loaded', function() {
    Event.observe(document, 'keypress', function(evt) {
       alert("In Keypress Function");
    });
});

, , . AFAIK, keypress .

+3

Try wrapping the input with a form element. If this does not help, warp it into a div and attach a handler to the div.

0
source

btw, for tabs, esc, etc. you need to use keyup, not keypress. At least for Safari, it took me a while to figure out what that means.

See for yourself by adding this line: alert ("Key :" + evt.keyCode);to anon. function. Only glyphs will be answered.

- Dave

0
source

Source: https://habr.com/ru/post/1704496/


All Articles