Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Javascript > Can I assign an event to a global variable?

Reply
Thread Tools

Can I assign an event to a global variable?

 
 
TriFuFoos@gmail.com
Guest
Posts: n/a
 
      10-04-2007
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!

 
Reply With Quote
 
 
 
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-04-2007
http://www.velocityreviews.com/forums/(E-Mail Removed) wrote:
> 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
--
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$(E-Mail Removed)>
 
Reply With Quote
 
 
 
 
TriFuFoos@gmail.com
Guest
Posts: n/a
 
      10-04-2007
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!

 
Reply With Quote
 
Thomas 'PointedEars' Lahn
Guest
Posts: n/a
 
      10-04-2007
(E-Mail Removed) wrote:
> 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
--
var bugRiddenCrashPronePieceOfJunk = (
navigator.userAgent.indexOf('MSIE 5') != -1
&& navigator.userAgent.indexOf('Mac') != -1
) // Plone, register_function.js:16
 
Reply With Quote
 
Lee
Guest
Posts: n/a
 
      10-04-2007
(E-Mail 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.


--

 
Reply With Quote
 
Peter Michaux
Guest
Posts: n/a
 
      10-05-2007
On Oct 4, 3:13 pm, Thomas 'PointedEars' Lahn <(E-Mail Removed)>
wrote:
> (E-Mail Removed) wrote:
> > 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

 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 1 10-25-2006 06:50 PM
FWSM/PIX and Dynamic PAT using global IP range vs. global interface vs. global IP Hoffa Cisco 0 10-25-2006 01:04 PM
How can I assign event handlers externaly? glevik@gmail.com Javascript 1 04-14-2005 11:20 PM
Using the __builtins__ module to assign new global variables Opinder Python 1 03-02-2004 12:49 AM
It's a bug!....but is it me or the script? (Global var assign) Halfdan Holger Knudsen Python 3 11-19-2003 04:25 PM



Advertisments