Advertisement

What does this do? (JavaScript)

Started by January 13, 2005 08:39 PM
10 comments, last by mike25025 20 years, 1 month ago

function out(element)
{
   eval( "document." + element + ".src = \"nav_\" + element + \"_out.gif\"" )
}

Why do you use the escape quotations in this function? The element being passed is the name of an image.
the code could be

function out(element){   eval( "document." + element + ".src = \"nav_" + element + "_out.gif\"" )}


and still do the samething
Advertisement
Ok, but why does that work too?
out("some_element_name");

is equal to

document.some_element_name.src = "nav_some_element_name_out.giv";

heres what the interperter sees

"document." + element + ".src = \"nav_" + element + "_out.gif\""
"document.some_element_name" + ".src = \"nav_" + element + "_out.gif\""
"document.some_element_name.src = \"nav_" + element + "_out.gif\""
"document.some_element_name.src = \"nav_some_element_name" + "_out.gif\""
"document.some_element_name.src = \"nav_some_element_name_out.gif\""
document.some_element_name.src = "nav_some_element_name_out.gif"
It dynamically changes an image named <element>
Not the best way to do it though.
So, why is the interpreter getting rid of the quotation marks around the plus signs first?

Advertisement
How would you do it, Prototype?
There are no quotation marks around the plus signs :)
Ok, why are the plus signs done away with first? ;)
Quote:
Original post by Chris41411
How would you do it, Prototype?

I would at least preload the images into an array, and use the more compliant GetElementById to retrieve the image object. But my JScript is kinda rusty though.

This topic is closed to new replies.

Advertisement