Re: Why 'event' is not defined in Mozilla
Available news archives: comp.lang.tcl - comp.lang.python - comp.security.firewalls - sci.crypt - comp.lang.php - comp.lang.javascript
Google
 
Web news.hping.org


comp.lang.javascript archive

Re: Why 'event' is not defined in Mozilla

From: Michael Winter <m.winter@blueyonder.co.uk>
Date: Fri Nov 25 2005 - 16:35:48 CET

On 25/11/2005 13:55, prabhdeep@gmail.com wrote:

> Can somebody explain, why in following code, i get "event not defined"
> error

Thomas commented on many problems, though omitted the cause of this
specific issue. An oversight, probably.

[snip]

> doument.getElementById('id1').addEventListener('mousedown', function(){
> click(sMenu, event); }, false);

The W3C event model, as implemented by Mozilla and others (excluding
IE), does not define a global event object. Instead, the object is
passed to listeners.

   var element;

   if(document.getElementById
    && (element = document.getElementById('id1'))
    && element.addEventListener)
   {
     element.addEventListener('mousedown', function(event) {
       click(sMenu, event);
     }, false);
   }

Notice the addition of an argument in the function expression.

Presumably, click is a user-defined function.

> But at the same time code as following works just fine

[snip]

> onkeypress="return isNumberInput(this, event);" />

When a browser encounters intrinsic event attributes, the value of that
attribute effectively becomes the body of a function object created
internally by the browser.

In IE, this transformation is similar to:

   element.onkeypress = function() {
     return isNumberInput(this, event);
   };

As it defines a global event object, the 'event' identifier will resolve
to that global property.

In browsers that implement the W3C event model, the transformation is
similar to the function expression I posted earlier:

   element.onkeypress = function(event) {
     return isNumberInput(this, event);
   };

In this way, the two models are markedly different, but interoperable
(in this instance).

Mike

-- 
Michael Winter
Prefix subject with [News] before replying by e-mail.
Received on Sat Dec 3 04:30:12 2005