M
McKirahan
I said I wouldn't use "eval()" anymore but I need help to do it.
Below is some stripped-down code (4 lines; watch for word-wrap) extracted
from USGA.COM that preloads images:
main_nav_home_F1 = new Image(153,21); main_nav_home_F1.src =
"images/main_nav_home.gif";
main_nav_home_F2 = new Image(153,21); main_nav_home_F2.src =
"images/main_nav_home_F2.gif";
main_nav_about_F1 = new Image(153,21); main_nav_about_F1.src =
"images/main_nav_about.gif";
main_nav_about_F2 = new Image(153,21); main_nav_about_F2.src =
"images/main_nav_about_F2.gif";
Since there are two variations of 20 images, I thought a loop would be more
efficient.
The following works (I think) but it uses "eval()".
<script type="text/javascript">
if (document.images) {
var imgs = new Array();
imgs[0] = "home";
imgs[1] = "about";
// imgs[#] = etc.
var what = "";
for (var i=0; i<imgs.length; i++) {
what = "main_nav_" + imgs;
eval(what + "_F1 = new Image(153,21); " + what + "_F1.src =
\"images/" + what + ".gif\"");
eval(what + "_F2 = new Image(153,21); " + what + "_F2.src =
\"images/" + what + "_F2.gif\"");
}
}
</script>
I have 2 questions:
1) How would I eliminate the use of "eval()"?
2) Is there a better way to do this?
Thanks in advance.
P.S. I'm not with the USGA; I'm just considering the technique they used.
Below is some stripped-down code (4 lines; watch for word-wrap) extracted
from USGA.COM that preloads images:
main_nav_home_F1 = new Image(153,21); main_nav_home_F1.src =
"images/main_nav_home.gif";
main_nav_home_F2 = new Image(153,21); main_nav_home_F2.src =
"images/main_nav_home_F2.gif";
main_nav_about_F1 = new Image(153,21); main_nav_about_F1.src =
"images/main_nav_about.gif";
main_nav_about_F2 = new Image(153,21); main_nav_about_F2.src =
"images/main_nav_about_F2.gif";
Since there are two variations of 20 images, I thought a loop would be more
efficient.
The following works (I think) but it uses "eval()".
<script type="text/javascript">
if (document.images) {
var imgs = new Array();
imgs[0] = "home";
imgs[1] = "about";
// imgs[#] = etc.
var what = "";
for (var i=0; i<imgs.length; i++) {
what = "main_nav_" + imgs;
eval(what + "_F1 = new Image(153,21); " + what + "_F1.src =
\"images/" + what + ".gif\"");
eval(what + "_F2 = new Image(153,21); " + what + "_F2.src =
\"images/" + what + "_F2.gif\"");
}
}
</script>
I have 2 questions:
1) How would I eliminate the use of "eval()"?
2) Is there a better way to do this?
Thanks in advance.
P.S. I'm not with the USGA; I'm just considering the technique they used.