473,583 Members | 3,242 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Why 'event' is not defined in Mozilla

Hi,

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

funcTest(sMenu)
{
doument.getElem entById('id1'). addEventListene r('mousedown', function(){
click(sMenu, event); }, false);
}
But at the same time code as following works just fine

<input type="text" name="gadgets" id="gadgets" size="2" value="0"
onchange="funcC hg();"

onkeypress="ret urn isNumberInput(t his, event);" />

Thanks
Prabh

Nov 25 '05 #1
15 21343
pr*******@gmail .com wrote:
Can somebody explain, why in following code, i get "event not defined"
error

funcTest(sMenu) ^^^^^^^^[1] {
doument.getElem entById('id1'). addEventListene r('mousedown', function(){ [2]^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^^^^^^^^^^^^^^^ ^[3][4] ^^^^^^^^^^^ click(sMenu, event); }, false); ^^^^^^^^^^^^^^^ ^^^^^^^[4] }
But at the same time code as following works just fine

<input type="text" name="gadgets" id="gadgets" size="2" value="0" ^^^^^^^^^^^[5] onchange="funcC hg();" ^^^^^^^^^[6]
onkeypress="ret urn isNumberInput(t his, event);" />

^^^^^^^^^^^^^^[7] ^[8]
[1] There is no user-defined funcTest() method probably causing a
problem here as the reserved word `function' is missing for a
FunctionDeclara tion or FunctionExpress ion.

[2] There is no `doument' reference in any known DOM and it is none
user-defined here.

[3] There is no element with ID `id1' here.

[4] You do not feature-test anything.

[5] `type="text"' is optional for `input' elements.

[6] There is no built-in funcChg() method and none user-defined here.

[7] There is no built-in isNumberInput() and none user-defined here.

[8] XHTML is not supported by Internet Explorer, and sending XHTML as
text/html to achieve non-achievable "HTML compatibility" according
to XHTML 1.0, Appendix C, is considered harmful.

<URL:http://jibbering.com/faq/#FAQ4_43>
<URL:http://www.pointedears .de/scripts/test/whatami>
<URL:http://hixie.ch/advocacy/xhtml>
<URL:http://validator.w3.or g>
<URL:http://jibbering.com/faq/>
PointedEars
Nov 25 '05 #2
On 25/11/2005 13:55, pr*******@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.getElem entById('id1'). addEventListene r('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.get ElementById
&& (element = document.getEle mentById('id1') )
&& element.addEven tListener)
{
element.addEven tListener('mous edown', 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="ret urn isNumberInput(t his, 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.onkeypr ess = function() {
return isNumberInput(t his, 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.onkeypr ess = function(event) {
return isNumberInput(t his, 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.
Nov 25 '05 #3
pr*******@gmail .com wrote:
Hi,

Can somebody explain, why in following code, i get "event not defined"
error
Beside all the syntax problems I will try to explain what is the problem
you are having (I think).
<input type="text" name="gadgets" id="gadgets" size="2" value="0"
onchange="funcC hg();"

onkeypress="ret urn isNumberInput(t his, event);" />
When you are in an event handler (such as onkeypress) there is an
implicit event object.
You can see it better in the next example

<script type="text/javascript">
function test1(event)
{
alert(event.tar get);
}
</script>
<button id="btn">Button </button>
<script type="text/javascript">
document.getEle mentById("btn") .onclick = test1;
</script>

Mozilla (and other standards compliant browsers) passes an event object
as the first parameter even though you did not pass this parameter
yourself explicitly.
Internet Explorer doesn't do this. However it has access to a global
event object in the window object.
The code above will not work in Internet Explorer, because the event
parameter will be undefined.
Therefore to make it work in both Mozilla and IE you will often see code
like this:

function test1(event)
{
event = event || window.event;
alert(event.tar get);
}

which first sees if the event parameter is defined and if not it will
use the global event object.

funcTest(sMenu)
{
doument.getElem entById('id1'). addEventListene r('mousedown', function(){
click(sMenu, event); }, false);
}


In this example there simply is no event object in scope for Mozila.
Internet Explorer uses the global event object.
To make this work your could do something like this:

function funcTest(event, sMenu)
{
doument.getElem entById('id1'). addEventListene r('mousedown', function(){
click(sMenu, event); }, false);
}

And you have to pass the event object yourself when calling funcTest.
Nov 25 '05 #4
Michael Winter wrote:
On 25/11/2005 13:55, pr*******@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.


No, that was deliberate. The OP is complaining about non-working code,
and provides an example that he says is working. However, he is showing
neither syntactically, let alone semantically, correct code nor a working
example.

Your correct explanation is based on the kind assumption that he used
different and otherwise correct code.
PointedEars
Nov 25 '05 #5
Michael Winter wrote:
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.get ElementById
&& (element = document.getEle mentById('id1') )
&& element.addEven tListener)
{
element.addEven tListener('mous edown', function(event) {
click(sMenu, event);
}, false);
}

Notice the addition of an argument in the function expression.
I have not tested it but I do think this will break in IE. The
argument the event listener is called would be 'undefined' and
scoping would force a reference to `event' within the listener
to evaluate to the value of the named argument instead of the
global event object. CMIIW, but I think the argument must be
named different from `event', and then it should be

click(sMenu, event || differentNamedA rgument);

or

click(sMenu, differentNamedA rgument || event);
onkeypress="ret urn isNumberInput(t his, 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.onkeypr ess = function() {
return isNumberInput(t his, event);
};


It is so in _all_ UAs (I know of), because `event' is defined in intrinsic
event _handler_ attribute values for those UAs. There is no difference in
IE.
As it defines a global event object, the 'event' identifier will
resolve to that global property.
No, the reason is a different one. See above.
In browsers that implement the W3C event model, the transformation is
similar to the function expression I posted earlier:

element.onkeypr ess = function(event) {
return isNumberInput(t his, event);
};


Very much "similar", indeed. See above.
PointedEars
Nov 25 '05 #6
On 25/11/2005 19:24, Thomas 'PointedEars' Lahn wrote:
Michael Winter wrote:
[snip]
var element;

if(document.get ElementById
&& (element = document.getEle mentById('id1') )
&& element.addEven tListener)
{
element.addEven tListener('mous edown', function(event) {
click(sMenu, event);
}, false);
}
[snip]
I have not tested it but I do think this will break in IE.


It won't break, but it won't work either because IE doesn't implement
the addEventListene r method, so the if condition will, eventually,
evaluate to false. I assumed that the OP realised this, so I didn't comment.

[snip]
In IE, this transformation is similar to:

element.onkeypr ess = function() {
return isNumberInput(t his, event);
};


It is so in _all_ UAs (I know of), because `event' is defined in
intrinsic event _handler_ attribute values for those UAs. There is
no difference in IE.


There is. Microsoft have always described their event object as a
property of the global (or rather, window) object, though it is only
available when an event is in progress.

An example, though not necessarily a conclusive one, demonstrates that
IE produces a function wrapper like the one displayed above.

<button onclick="alert( this.onclick);" >Click me!</button>

It should output:

function anonymous()
{
alert(this.oncl ick);
}

On the other hand, Mozilla and Opera output:

function onclick(event) {
alert(this.oncl ick);
}

(even though the latter also implements IE's event model). Furthermore,
Mozilla have documented this as its actual behaviour[1].
As it defines a global event object, the 'event' identifier will
resolve to that global property.


No, the reason is a different one. See above.


I think it will be difficult to prove through code precisely what IE's
behaviour is. Though,

window.event == event

will evaluate to false, so will

window.event == window.event

which is bizarre, to say the least. It's possible that IE modifies the
scope chain to include the event object somewhere between the global
object and the activation/variable object, but I doubt it.

[snip]

Mike
[1] <http://developer.mozil la.org/en/docs/DOM:element#Eve nt_Handlers>

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Nov 25 '05 #7
Michael Winter wrote:
On 25/11/2005 19:24, Thomas 'PointedEars' Lahn wrote:
Michael Winter wrote:
var element;

if(document.get ElementById
&& (element = document.getEle mentById('id1') )
&& element.addEven tListener)
{
element.addEven tListener('mous edown', function(event) {
click(sMenu, event);
}, false);
}
[snip]
I have not tested it but I do think this will break in IE.
It won't break, but it won't work either because IE doesn't implement
the addEventListene r method,


True. For the time being :)
so the if condition will, eventually, evaluate to false. I assumed that
the OP realised this, so I didn't comment.
ACK
In IE, this transformation is similar to:

element.onkeypr ess = function() {
return isNumberInput(t his, event);
};


It is so in _all_ UAs (I know of), because `event' is defined in
intrinsic event _handler_ attribute values for those UAs. There is
no difference in IE.


There is.


Maybe I did not make myself clear. In the above code, the `event' reference
is indeed IE specific. However, the above code is your transformation of

onkeypress="ret urn isNumberInput(t his, event);"

And that is, as is, fully cross-browser (across all event-handling HTML
UAs I know of, meaning being DOM Level 0). The `event' _there_ does _not_
refer to a global event object in IE (well, maybe it does indirectly, but
to think about that is futile).
On the other hand, Mozilla and Opera output:

function onclick(event) {
alert(this.oncl ick);
}

(even though the latter also implements IE's event model). Furthermore,
Mozilla have documented this as its actual behaviour[1].
So?
As it defines a global event object, the 'event' identifier will
resolve to that global property.


No, the reason is a different one. See above.


I think it will be difficult to prove through code precisely what IE's
behaviour is. Though,

window.event == event

will evaluate to false,


Indeed, as it should.
so will

window.event == window.event

which is bizarre, to say the least.


Indeed, it should not. Maybe because it is a host object.
PointedEars
Nov 25 '05 #8
On 25/11/2005 22:30, Thomas 'PointedEars' Lahn wrote:

[snip]
onkeypress="ret urn isNumberInput(t his, event);"
[snip]
The `event' _there_ does _not_ refer to a global event object in IE
Is that supposed to be a statement of fact,
(well, maybe it does indirectly, but to think about that is futile).


or a statement that, for all intents and purposes, may as well be
considered fact? I can accept the latter for the sake of simplicity, but
not the former unless you can prove otherwise.

[MLW:]
window.event == event

will evaluate to false,


Indeed, as it should.


Even if event is a property of an object in the scope chain other than
the global object, I would find it hard to believe that its value is a
reference to a separate object.

I maintain (unless someone proves to the contrary) that they are not
only references to the same object, but the same property and the result
of the comparison above is a symptom of the behaviour exhibited when
window.event is compared to itself.

[snip]

Mike

--
Michael Winter
Prefix subject with [News] before replying by e-mail.
Nov 25 '05 #9
Michael Winter wrote:
<snip>
I maintain (unless someone proves to the contrary) that
they are not only references to the same object, but the
same property and the result of the comparison above is a
symptom of the behaviour exhibited when window.event is
compared to itself.


I would put my money on this as well. However, an experiment to find out
where this 'event' property is located might be performed. Say you have
an intrinsic event handler on a form control, where IE will add the form
and the document to the scope chain (as I recall, and without going back
and finding the thread of scope augmentation tests that were done a few
years ago). If a property was added to any object on the scope chain
with the name 'even' that property would mask this questionable 'event'
property if it was on any object lower in the scope chain.

If the real event object can not be masked by such a property then it
must exist at the top of the scope chain (Activation/Variable object or
similar). On the other hand, if it turned out that the document was the
lowest object on the scope chain that masked the event object it might
be reasonable to conclude that the unqualified identifier - event - did
indeed refer to the property of the window/global object.

I might try this out tomorrow, though I expect to be beaten to posting
result by one of you :)

Richard.
Nov 26 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
2287
by: Martin Zuber | last post by:
Hello, I have found following problem: When I define event handler for EVT_NOTEBOOK_PAGE_CHANGED for wxNotebook, the content of the wxNotebook disappears (on all pages). For ex. I have two pages - one with some wxTextCtrls, second with some wxGrid. The dialog has been created using the Boa Constructor (0.2.3 and 0.2.7). I have two files:...
3
39683
by: yzzzzz | last post by:
Hi I have: <div onkeypress="go(event)">...</div> and: function go(event) { alert(event.keyCode); }
4
4903
by: Pai | last post by:
hello there, I am trying to rersize the window it works find in IE but doea not work with mozilla window.attachEvent(onload,MWSOnLoad); window.onload = function (MWSOnLoad) { alert('hello'); window.resizeTo(810,750); top.outerWidth=810;
2
1603
by: JeeWee | last post by:
Hi all, I'm looking for a way to "bind" multiple eventhandler function to the same event. In other languages this can often be done by using the += operator, unfortunately this doesn't seem to work. My (working) code for assigning an eventhandler for some form elements is: for( var i = 0; i < document.formx.elements.length; i++ ) {...
6
4560
by: Brian | last post by:
Hi everyone, I'm writing a function (in javascript) that needs to do one thing if the page has not loaded, and another (different) thing if the page has already loaded. I'm looking for a way to tell if the window.onload event has already fired. I cannot edit the onload event handler itself, and my function can only exist in an external...
17
61419
by: Mike Gratee | last post by:
Is it possible to use JavaScript to cause the browser to click a link on a page and have the browser act exactly like the user had clicked on the link directly? In other words, I need to programmatically issue a JavaScript statement which causes the browser to act just like the user clicked on a link in my page. And if that link has an...
1
4436
by: sundog2000 | last post by:
I am writing my first VB.net program and I am struggling to figure out how to attach an event to a method, when the event is part of an interface that the class implements. I have declared a delegate function, a public event, and the class object (g_pFeedManager) is first dim'ed as an interface. Later it is new'd at a class that implements...
41
4278
by: JohnR | last post by:
In it's simplest form, assume that I have created a usercontrol, WSToolBarButton that contains a button. I would like to eventually create copies of WSToolBarButton dynamically at run time based on some initialization information obtained elsewhere. Basically, I'm going to create my own dynamic toolbar where the toolbarbuttons can change. ...
5
2397
by: jeet_sen | last post by:
Hi, My external javascript test.js contains a variable definition var a = "Hello,world!!"; I dynamically loaded the script test.js using the following fucntion: function loadScript(url) { var e = document.createElement("script"); e.src = url; e.type="text/javascript";
3
2535
by: rahulgupta | last post by:
i have a textbox and a save_btn which is a hyperlink. when ever enter key is pressed there is a javascript which check if the name in the textbox contains any special characters. but when we press enter it do page postback and does not check for special characters. i have written a javascript function which stops the post back on enter...
0
7821
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language...
0
8172
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
8317
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
5695
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
5369
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert...
0
3813
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
2326
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1422
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
1151
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.