473,405 Members | 2,310 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,405 software developers and data experts.

Can I assign an event to a global variable?

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!

Oct 4 '07 #1
6 2602
Tr*******@gmail.com 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, <f8*******************@news.demon.co.uk>
Oct 4 '07 #2
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!

Oct 4 '07 #3
Tr*******@gmail.com 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
Oct 4 '07 #4
Lee
Tr*******@gmail.com 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.
--

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

Oct 5 '07 #6
Peter Michaux said the following on 10/4/2007 10:56 PM:
On Oct 4, 3:13 pm, Thomas 'PointedEars' Lahn <PointedE...@web.de>
wrote:
>TriFuF...@gmail.com 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.
<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/
Oct 5 '07 #7

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

Similar topics

3
by: Halfdan Holger Knudsen | last post by:
Hey here's a relatively simple question...the code is for a mock-up passwd, usrname dict storagesystem. Why doesn't the variable enc behave globally (the admin function turns up a "local var...
4
by: Eric | last post by:
How can I dynamically assign an event to an element? I have tried : (myelement is a text input) document.getElementById('myelement').onKeyUp = "myfnc(param1,param2,param3)"; ...
9
by: VK | last post by:
My original idea of two trains, however pictural it was, appeared to be wrong. The truth seems to be even more chaotic. IE implements its standard down-up model: any mouse event goes from the...
16
by: sneill | last post by:
How is it possible to take the value of a variable (in this case, MODE_CREATE, MODE_UPDATE, etc) and use that as an object property name? In the following example I want 'oIcon' object to have...
1
by: Bruce Lawrence | last post by:
I have a textbox on a form that I want to use to display a public variable. How would I go about doing this? Could I use a label as well?
3
by: Pavils Jurjans | last post by:
Hello, I am looking for solution to assign the Session.onEnd event handler dynamically, at runtime, without using global.asax file. I am a bit sceptic wether that is possible, however I thought...
4
by: mflll | last post by:
I am looking into the different techniques of handling arrays of edit boxes in Java Script. The first program below works fine. However, are there better ways of doing this, where the person...
5
by: Jason | last post by:
Hello, I am trying to dynamically create a table, then set its <td>'s onclick: var table = document.createElement("table"); var row = table.insertRow(-1); var td = row.insertCell(-1);...
2
by: sorobor | last post by:
dear sir .. i am using cakephp freamwork ..By the way i m begener in php and javascript .. My probs r bellow I made a javascript calender ..there is a close button ..when i press close button...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
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...
0
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
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...

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.