Search code examples
javascripteventsdojodom-eventsonkeypress

Event is undefined in Firefox during dojo.connect onKeyPress


I have the following declarative js/dojo code running properly in Chrome:

<div dojoType="dijit.form.TextBox">
    <script type="dojo/connect" event="onKeyPress">
        console.log(event);
    </script>
</div>

(http://jsfiddle.net/pfSXF/)

Typing some characters into the TextBox I get some KeyboardEvents in console output.

Exactly the same code doesn't run in Firefox. I get the following error in console output:

event is not defined

Fetching the object from window doesn't work either (same error output):

<div dojoType="dijit.form.TextBox">
    <script type="dojo/connect" event="onKeyPress">
        if(!event) {
            event = window.event;
        }

        console.log(event);
    </script>
</div>

(http://jsfiddle.net/pfSXF/1/)

What is the trick?


Solution

  • The declarative dojo code prevents Firefox from passing the event object to the connected function - one has to do that by hand via args attribute:

    <div dojoType="dijit.form.TextBox">
        <script type="dojo/connect" event="onKeyPress" args="event">
            console.log(event);
        </script>
    </div>
    

    (http://jsfiddle.net/pfSXF/2/)