1

Consider the following code:

<input id="myinput" type="text" size="40" onkeydown="myFunction()">

function myFunction() {
  console.log(event);
}

event is a global object and is known inside the event handler myFunction. However, Firefox throws an error:event is not defined while Chrome and IE output KeyboardEvent {...}. This is one of the most crazy things I have met. Any satisfactory explanation?

2
  • please try this: ` function myFunction(event) {console.log(event);} ` is this work? May 21, 2016 at 20:52
  • 1
    event being a global variable is a shitty IE invention that is now deprecated but is supported for backwards-compatibility in various browser. Don't do this. Pass the event object through explicitly.
    – Bergi
    May 21, 2016 at 21:24

1 Answer 1

2

Firefox's KeyboardEvent() expect event passed to the function Try this

<input id="myinput" type="text" size="40" onkeydown="myFunction(event)">

function myFunction(event){
    if(typeof event === 'undefined')
    {
        event = window.event;
    }

console.log(event);

}

Not the answer you're looking for? Browse other questions tagged or ask your own question.