eval, alternative

G

Guest

Hi,

As you can see from the following code, I am trying to manipulate a
large number of layers. I have read many times in the past how the
"Eval" function should be used very sparingly as it can make code run
slowly. The following works fine on my PC, however I do not know what
will happen on other platforms, so is there an alternative to "Eval"
in this instance.

for(var i=1;i<505;i++){
eval('document.all.mun'+ i +'.style.visibility=\'hidden\'')
}

I have experimented with the following but cannot get it to work

document.all.mun.style.visibility='hidden'
 
S

Stevo

Hi,

As you can see from the following code, I am trying to manipulate a
large number of layers. I have read many times in the past how the
"Eval" function should be used very sparingly as it can make code run
slowly. The following works fine on my PC, however I do not know what
will happen on other platforms, so is there an alternative to "Eval"
in this instance.

for(var i=1;i<505;i++){
eval('document.all.mun'+ i +'.style.visibility=\'hidden\'')
}

I have experimented with the following but cannot get it to work

document.all.mun.style.visibility='hidden'


var found=document.getElementById("mun"+i);
if(found)
found.style.visibility="hidden";

This makes it compatible with other platforms too, by avoiding
document.all usage.
 
G

Guest

As you can see from the following code, I am trying to manipulate a
large number of layers. I have read many times in the past how the
"Eval" function should be used very sparingly as it can make code run
slowly. The following works fine on my PC, however I do not know what
will happen on other platforms, so is there an alternative to "Eval"
in this instance.
for(var i=1;i<505;i++){
        eval('document.all.mun'+ i +'.style.visibility=\'hidden\'')
}
I have experimented with the following but cannot get it to work
document.all.mun.style.visibility='hidden'


var found=document.getElementById("mun"+i);
if(found)
   found.style.visibility="hidden";

This makes it compatible with other platforms too, by avoiding
document.all usage.- Hide quoted text -

- Show quoted text -


many thanks, works well!
 
L

Lasse Reichstein Nielsen

I have read many times in the past how the
"Eval" function should be used very sparingly as it can make code run
slowly.

Indeed. Eval is evil, both for performance and maintainability.
The faster the javascript engines get, the more performance optimizations
they use, and an eval in the same scope means that you can kiss any static
analysis goodbye.
The following works fine on my PC, however I do not know what
will happen on other platforms, so is there an alternative to "Eval"
in this instance.

It will work the same if run in the same browser :)
for(var i=1;i<505;i++){
eval('document.all.mun'+ i +'.style.visibility=\'hidden\'')

Stevo already told you what to do to avoid both eval and document.all.
Just for the record, the way to avoid eval alone (which generalizes
to all other computed property names) is square bracket notation:
document.all["mun"+i].style.visibility='hidden';


/L
 
D

Dr J R Stockton

In comp.lang.javascript message <095992ad-f590-4f7d-8545-2ff82922a2b1@t3
9g2000prh.googlegroups.com>, Tue, 11 Nov 2008 06:45:06,
(e-mail address removed) posted:
document.all.mun.style.visibility='hidden'


You already have a better answer; but what you were attempting is more
likely to be achieved by something like

document.all["mun"+i].style.visibility='hidden'
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,778
Messages
2,569,605
Members
45,238
Latest member
Top CryptoPodcasts

Latest Threads

Top