/    Sign up×
Community /Pin to ProfileBookmark

javascript garbage collector?

[QUOTE]

var ie=document.all && window.showModalDialog && navigator.appName==”Microsoft Internet Explorer”;

if (document.getElementsByTagName) {

var f = document.getElementsByTagName(“LI”);

for (i=0; i<f.length; i++) {
if (f[i].className == “sublevel”){
var [B]toggle[/B]=(ie)?f[i].childNodes[1]:f[i].nextSibling.nextSibling;

f[i].onmouseover=function() {
[COLOR=DarkOrange]toggle.style.display=’block’;[/COLOR]

[COLOR=Sienna] alert(toggle.nodeName);
alert(toggle.style.display);
[/COLOR]

// this works for IE: [B]this.childNodes[1].style.display=’block’;[/B]
// this works for FF/Op: [B]this.nextSibling.nextSibling.style.display=’block’;[/B]
// but why wont the above work??
};

f[i].onmouseout=function() {
[COLOR=DarkOrange]toggle.style.display=’none’;[/COLOR]
};

.
.
.

[/QUOTE]

Im in the middle of building a unique dropdown menu but have come accross some weird variable problem. To overcome the different normalized DOM versions between IE and FF/Opera I introduce a variable called ie. This is used to assign the next element (in my case a <UL> element) to the [B]toggle[/B] variable. However when assigning an “onmouseover” event to an <LI> element the toggle reference variable will not perform the following action:
[COLOR=DarkOrange]toggle.style.display=’block’;[/COLOR]

but, I know it is holding the correct reference and is able to execute as the following actions work:
[COLOR=Sienna] alert(toggle.nodeName);
alert(toggle.style.display);
[/COLOR]

why is this? and how do I overcome it?
Thanks in advance for any help.

to post a comment
JavaScript

6 Comments(s)

Copy linkTweet thisAlerts:
@MongusJul 04.2006 — Looks like you're having a problem with the scope of the toggle variable. You're creating a closure by creating a function that uses the toggle variable. The problem is that there is no block level scope for JavaScript variables so with every loop iteration you're changing the value of toggle within all of the functions you created.

I think this is closer to what you're trying to do: for (i=0; i&lt;f.length; i++) {
if (f[i].className == "sublevel"){
f[i].onmouseover=function() {
var toggle=(ie)?this.childNodes[1]:this.nextSibling.nextSibling;
toggle.style.display='block';
//alert(toggle.nodeName);
//alert(toggle.style.display);

<i> </i> // this works for IE: this.childNodes[1].style.display='block';
<i> </i> // this works for FF/Op: this.nextSibling.nextSibling.style.display='block';
<i> </i> // but why wont the above work??
<i> </i> };

<i> </i> f[i].onmouseout=function() {
<i> </i> var toggle=(ie)?this.childNodes[1]:this.nextSibling.nextSibling;
<i> </i> toggle.style.display='none';
<i> </i> };
<i> </i> }
<i> </i>}
Copy linkTweet thisAlerts:
@MongusJul 04.2006 — Just tried it in Firefox and found it didn't work properly. I had to change the toggle declaration to this:var toggle=this.childNodes[1];
Now it works in Firefox, IE, and Opera without any browser specific ugliness. ?
Copy linkTweet thisAlerts:
@tabzterauthorJul 04.2006 — Thanks for the help. I kinda figured how to get it to work, that was the easy part, finally settling with

f[i].onmouseover=function() {

(ie)?

this.childNodes[1].style.display='block':

this.nextSibling.nextSibling.style.display='block';

};



but I was muffled as to why the original, much simpler, code wasnt working.

You mentioned something about closure - what is this and how did it prevent the functions from working?

Thankyou
Copy linkTweet thisAlerts:
@MongusJul 04.2006 — There are several sites out there that can describe closures better than I but I'll give it a shot.

A closure occurs when you create a function that uses a variable from the containing function's scope. The variable is still available to the enclosed function even after the original function returns. If you create more than one function that use the same variable they will share it. It vaguely resembles an instance variable if you use Java or C++.

Your original code was only defining one variable named toggle. As you iterated through your loop the toggle variable kept being updated. All of the functions you created while in the loop were using the same toggle variable. The way you set it up you intended to keep a separate toggle variable for each <li> but every <li>'s mouse events ended up using the last value that was assigned to the toggle variable.

Did that make sense or did I just confuse you more? ?
Copy linkTweet thisAlerts:
@tabzterauthorJul 04.2006 — that made a alot of sense, thanks alot - you should be a teacher.

appreciate the help, time and effort you put in to help me.
Copy linkTweet thisAlerts:
@MongusJul 04.2006 — Glad I could help.

I'll have to disagree about being a teacher though. Eventually I can get the point across but I'm really not very good at it. Tutor maybe but not a teacher. I'm making way too much money doing web development to consider a career change anyway. ?
×

Success!

Help @tabzter spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.24,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...