Connecting Tech Pros Worldwide Forums | Help | Site Map

Can I assign an event to a global variable?

TriFuFoos@gmail.com
Guest
 
Posts: n/a
#1: Oct 4 '07
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!


Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#2: Oct 4 '07

re: Can I assign an event to a global variable?


TriFuFoos@gmail.com wrote:
Quote:
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.
Quote:
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.
Quote:
alert(globalEvent.type);
Given the above assignment, this should throw a TypeError exception already
in the MSHTML DOM.
Quote:
setTimeout(function(){unhideDiv()}, 2000 );
Should be

window.setTimeout(...);
Quote:
}
>
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.
Quote:
}
>
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.
Quote:
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.
Quote:
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.
Quote:
If anyone could shed some light on this issue it'd be greatly
appreicated thanks!

HTH

PointedEars
--
Prototype.js was written by people who don't know javascript for people
who don't know javascript. People who don't know javascript are not
the best source of advice on designing systems that use javascript.
-- Richard Cornford, cljs, <f806at$ail$1$8300dec7@news.demon.co.uk>
TriFuFoos@gmail.com
Guest
 
Posts: n/a
#3: Oct 4 '07

re: Can I assign an event to a global variable?


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!

Thomas 'PointedEars' Lahn
Guest
 
Posts: n/a
#4: Oct 5 '07

re: Can I assign an event to a global variable?


TriFuFoos@gmail.com wrote:
Quote:
I'm not very knowledgeable with how event listeners or handlers work?
Is that a question?
Quote:
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.
Quote:
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.
Quote:
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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
Lee
Guest
 
Posts: n/a
#5: Oct 5 '07

re: Can I assign an event to a global variable?


TriFuFoos@gmail.com said:
Quote:
>
>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.


--

Peter Michaux
Guest
 
Posts: n/a
#6: Oct 5 '07

re: Can I assign an event to a global variable?


On Oct 4, 3:13 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Quote:
TriFuF...@gmail.com wrote:
Quote:
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.
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

Randy Webb
Guest
 
Posts: n/a
#7: Oct 5 '07

re: Can I assign an event to a global variable?


Peter Michaux said the following on 10/4/2007 10:56 PM:
Quote:
On Oct 4, 3:13 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
Quote:
>TriFuF...@gmail.com wrote:
Quote:
>>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.
>
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.
<sarcasm>
Don't use c.l.j in comp.lang.javascript, it confuses Thomas and he
thinks you are referring to the comp.lang.java heirarchy.
</sarcasm>

Your advice is good though :)

--
Randy
Chance Favors The Prepared Mind
comp.lang.javascript FAQ - http://jibbering.com/faq/index.html
Javascript Best Practices - http://www.JavascriptToolbox.com/bestpractices/
Closed Thread