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?

Unknown developer
  • 5,414
  • 13
  • 52
  • 100
  • please try this: ` function myFunction(event) {console.log(event);} ` is this work? – farhad goodarzi May 21 '16 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 '16 at 21:24

1 Answers1

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);

}
Bassam Rubaye
  • 302
  • 1
  • 4