Can I assign an event to a global variable?

T

TriFuFoos

Hi there, I was wondering if anyone knew if/how to assign an event to
a global variable?

I tried to do it and IE 7 came back with an error saying "Member not
found"
My code looked similar to the following:

var globalEvevnt;
function showPopup(event){
globalEvent = event;
alert(globalEvent.type);
setTimeout(function(){unhideDiv()}, 2000 );

}



function unhideDiv(){
alert(globalEvent.type); //Member not found error found on this
line
}

I was wondering if I had declared the globalEvent = new Object();
would that make any difference? I thought everything in JS was an
object
so the event could be stored to one as well?

If anyone could shed some light on this issue it'd be greatly
appreicated thanks!
 
T

Thomas 'PointedEars' Lahn

I tried to do it and IE 7 came back with an error saying "Member not
found"
My code looked similar to the following:

Posting only similar code is likely not to be helpful in analyzing the
problem. Post exactly what you use instead, a URL for a test case if necessary.
var globalEvevnt;
function showPopup(event){
globalEvent = event;

You have not showed how showPopup() is called. If I assume that you have
used the Function object reference in an event handler assignment or in an
event listener addition call, the described problem could be explained as
follows:

Declaring `event' as a named argument of showPopup() modifies scope chain
resolution of `event' within showPopup to result in a reference to that
argument, and not to an Event object that may be later in the scope chain.
However, in the MSHTML DOM the Event object is not passed to the event
listener, and so `event' yields `undefined'. And `undefined' has no properties.

This can be fixed with

if (! event) event = window.event;

I recommend to use `e' instead of `event' for the argument identifier to
avoid confusion.
alert(globalEvent.type);

Given the above assignment, this should throw a TypeError exception already
in the MSHTML DOM.
setTimeout(function(){unhideDiv()}, 2000 );

Should be

window.setTimeout(...);
}

function unhideDiv(){
alert(globalEvent.type); //Member not found error found on this
line

window.alert(...);

Code should be posted so that it is unlikely to break on execution when
being wrapped to about 72 characters per line. Therefore, multi-line
comments are to be used instead of single-line comments in that case, unless
the comment is short enough.
}

I was wondering if I had declared the globalEvent = new Object();
<> would that make any difference?

Of course not. The reference to the newly created Object object would have
been overwritten by

globalEvent = event;

and the Object object would have been marked for Garbage Collection as there
would have been no more references to it.
I thought everything in JS was an object

Not everything; there are primitive data types as well. However, such a
value is not involved here.
so the event could be stored to one as well?

You can store a reference to an (Event) object in a variable. The provision
for that is that there is an (Event) object in the first place.
If anyone could shed some light on this issue it'd be greatly
appreicated thanks!


HTH

PointedEars
 
T

TriFuFoos

Thanks for your reply Thomas!

I'm not very knowledgeable with how event listeners or handlers work?
What difference does window.setTimeout as opposed to setTimeout do?

and I'm calling the showPopup like this:

<td onmouseover="showPopup(event);"></td>


What I'm also confused about is although you say
alert(globalEvent.type) should
give me an error, it works when I'm still in the showPopup method, and
yet
it fails when I call it again in unhideDiv()??

Thanks in advance!
 
T

Thomas 'PointedEars' Lahn

I'm not very knowledgeable with how event listeners or handlers work?

Is that a question?
What difference does window.setTimeout as opposed to setTimeout do?

setTimeout() is not a built-in method; it is a method of Window host objects
and it should be called so.

One is calling a known and well-supported (albeit proprietary)
function-property of the object `window' refers to, the other is calling a
property of the next object in the scope chain that has such a property,
provided that there is such an object and that this property can be called
(otherwise it throws a TypeError exception). In short, the former is more
obvious (as one can see at a glance by/for which object the method is
called), more efficient (as scope chain resolution is faster with a given
object reference), and less error-prone.
and I'm calling the showPopup like this:

<td onmouseover="showPopup(event);"></td>

That changes the meaning of the `event' named argument of showPopup(), of
course. I assumed that you used (DOM Level 0)

refToTDElObj.onmouseover = showPopup;

or (W3C DOM Level 2 Events)

refToTDElObj.addEventListener('...', showPopup, ...);

or (MSHTML DOM)

refToTDElObj.attachEvent('...', showPopup);

instead. As I said, how showPopup() is called is significant.
What I'm also confused about is although you say
alert(globalEvent.type) should
give me an error, it works when I'm still in the showPopup method, and
yet it fails when I call it again in unhideDiv()??

The reason for that is if you pass `event' in an event handler attribute
value, it refers (proprietarily) to the current Event object. Which means
that in the MSHTML DOM the value of the `event' argument (in showPopup) is
not `undefined'. Hence globalEvent.type there does not throw an exception.

The only explanation I have as to why the same lookup threw an exception in
unhideDiv() is that you have indeed a typo there --

| var globalEvevnt;

-- and that this typo is significant. Because that would mean that

globalEvent = event;

in showPopup() would attempt to add a property to an object in the scope
chain, which is not necessarily the Global Object. And then in unhideDiv(),

| globalEvent.type

would be resolved to

undefined.type

which would throw a TypeError. Unless you have posted code less similar to
the code you are actually using.

I recommend instead not to use globally available properties. Quickhack:

function unhideDiv(e)
{
window.alert(e.type);
}

function showPopup(e)
{
window.alert(e.type);
window.setTimeout(function(){ unhideDiv(e); }, 2000);
}


PointedEars
 
L

Lee

(e-mail address removed) said:
Thanks for your reply Thomas!

I'm not very knowledgeable with how event listeners or handlers work?
What difference does window.setTimeout as opposed to setTimeout do?

and I'm calling the showPopup like this:

<td onmouseover="showPopup(event);"></td>


What I'm also confused about is although you say
alert(globalEvent.type) should
give me an error, it works when I'm still in the showPopup method, and
yet
it fails when I call it again in unhideDiv()??

The problem is that when you assign an object to a variable,
you're really only assigning a reference to that object, not
making a copy of it. So the variable can only be used until
a new event occurs, replacing the event object with a new one.


--
 
P

Peter Michaux

Posting only similar code is likely not to be helpful in analyzing the
problem. Post exactly what you use instead, a URL for a test case if necessary.

Even better is posting a minimal, 30 lines or less, self-contained,
complete HTML page example that exhibits the problem and was written
with posting it to c.l.j in mind. A link to the same example online is
a nice touch.

Peter
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,819
Latest member
masterdaster

Latest Threads

Top