473,657 Members | 2,586 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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(globalEve nt.type);
setTimeout(func tion(){unhideDi v()}, 2000 );

}

function unhideDiv(){
alert(globalEve nt.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 2621
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(globalEve nt.type);
Given the above assignment, this should throw a TypeError exception already
in the MSHTML DOM.
setTimeout(func tion(){unhideDi v()}, 2000 );
Should be

window.setTimeo ut(...);
}

function unhideDiv(){
alert(globalEve nt.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.de mon.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.setTimeo ut as opposed to setTimeout do?

and I'm calling the showPopup like this:

<td onmouseover="sh owPopup(event); "></td>
What I'm also confused about is although you say
alert(globalEve nt.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.setTimeo ut 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="sh owPopup(event); "></td>
That changes the meaning of the `event' named argument of showPopup(), of
course. I assumed that you used (DOM Level 0)

refToTDElObj.on mouseover = showPopup;

or (W3C DOM Level 2 Events)

refToTDElObj.ad dEventListener( '...', showPopup, ...);

or (MSHTML DOM)

refToTDElObj.at tachEvent('...' , showPopup);

instead. As I said, how showPopup() is called is significant.
What I'm also confused about is although you say
alert(globalEve nt.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.typ e 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.typ e

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.setTimeo ut(function(){ unhideDiv(e); }, 2000);
}
PointedEars
--
var bugRiddenCrashP ronePieceOfJunk = (
navigator.userA gent.indexOf('M SIE 5') != -1
&& navigator.userA gent.indexOf('M ac') != -1
) // Plone, register_functi on.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.setTimeo ut as opposed to setTimeout do?

and I'm calling the showPopup like this:

<td onmouseover="sh owPopup(event); "></td>
What I'm also confused about is although you say
alert(globalEv ent.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...@we b.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...@we b.de>
wrote:
>TriFuF...@gmai l.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.javas cript, 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.javas cript 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
1890
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 referenced before assignment" error, when inputting 's')? In all likelyhood I've just managed to stare myself blind, so to anyone w/ a spare pair of eyes - the help would be appreciated. It's run under Python 2.3.0 in Win2k. T/x H PS: I know it's long...
4
12567
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)"; document.getElementById('myelement') = new Function("myfnc(param1,param2,param3)");
9
5727
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 deepest visible element to the top. By carefully studying fromElement and toElement properties, one can handle events on any point of their way up. NN/FF implements a "Russian hills" style: mouse events go first up->down (window->deepest...
16
25404
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 the properties: mode1, mode2, and mode3. This seems simple but I can't quite figure it out... Any ideas anyone?
1
15875
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
1911
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 maybe it is possible to have a global.asax file with Session_onEnd method, that merely calls some delegate, and that delegate is assigned by my web application at runtime. Maybe there is some example ready?
4
9680
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 writing the JavaScript doesn't have to pass the index in the "onChange" event name. I thought that one might be able to use "this.value" or compare this as
5
6481
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); td.onclick = myfunction; function myfunction(event) { alert(event);
2
3147
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 then the calender gone actually i want if i click outside off the calender then it should me removed ..How kan i do this ... Pls inform me as early as possible .. I am waiting for ur quick replay ...Here i attached the source code .... <!DOCTYPE...
0
8823
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. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
8605
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7321
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6163
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5632
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 into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
4151
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4301
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
1950
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1607
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 effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.