Jump to content

Javascript: Substitute A Var With It's Value


RandomClown

Recommended Posts

Hai!I was trying to write up a script & ran in to a problem.In the HTML page, I have:

<img name="FInfo" src="Link1">

Somewhere in the JavaScript, I have:

var FInfo="Link2";for(var i=0;i<document.images.length;i++){   document.images[i].src=document.images[i].name}

I want to somehow set images.src to FInfo's value, which is Link2Like so:

document.images[i].src=document.images[i].name   //  becomesdocument.images[i].src=FInfo   //  which I wanted to bedocument.images[i].src=Link2

I couldnt find anything on Google. Is there a way to do this?

Link to comment
Share on other sites

Yeah, that'll work. But what are you really trying to do. What's the big picture? I'm sure there's an easier way, possibly using an array, because you wouldn't go through this if it were just one image, right?Thing is, you have a problem to solve, and you came up with some code to solve it. The code doesn't work, so Synook shows you how to fix the code. What I'm saying is, there's probably a better way to solve the problem.I don't really know, but it's an educated guess.

Link to comment
Share on other sites

Hey it works! Thanks much!The reason for doing this is because I once googled "failsafe images" or something & found out that JS could replace the images if it did not appear.I wanted to make my own, so I tried to think of a unique way of coding it.The site I saw replaced all pictures with the same image.Mine gives each image it's own.Here is what I have:

<img name="PictureName" src="http://w3sch@@@@@@@@ls.invisionzone.com/style_images/6_logo.png"><img name="W3Schools" src="http://www.w3schools.com/images/h_logo.gif">

For all images I want to have backups for, I name the picture something.EX. Notice I screwed up the link for the 1st image.

<script language="javascript">// This JS has to be at the bottom of the page; Or at least after the last backed up image..// The backup of images I want backups for.		PictureName="http://w3schools.invisionzone.com/style_images/6_logo.png";		W3Schools="http://www.w3schools.com/images/h_logo.gif";for(var i=0;i<document.images.length;i++){	FS=new Image();	FS.src=document.images[i].src;	if(FS.height==0){		document.images[i].src=eval(document.images[i].name)	}}</script>

**EDIT**I was on Firefox when I did this. It worked great.IE seemed to fail.At the PictureName="link" part,IE reports: "Objects doesn't support this property or method"I have not looked it up yet, as I am a bit busy. I assume this is unfixable for us?

Link to comment
Share on other sites

IE seemed to fail.
IE has a funny little quirk regarding javascript variable names and names of elements. Consider the following example:
<html><body><input type="text" name="test" value="This is a test" /><script type="text/javascript">alert(test.value);</script></body></html>

Firefox will, understandably, throw an error. IE, on the other hand, will alert "This is a test".Try modifying your code as follows:

var img_PictureName="http://w3schools.invisionzone.com/style_images/6_logo.png";var img_W3Schools="http://www.w3schools.com/images/h_logo.gif";

And then:

document.images[i].src=eval("img_" + document.images[i].name);

Link to comment
Share on other sites

IE has a funny little quirk regarding javascript variable names and names of elements. Consider the following example:.......Firefox will, understandably, throw an error. IE, on the other hand, will alert "This is a test".Try modifying your code as follows:.......And then:.......
I tried your idea, but it still came out the same.IE is weird.Thanks for the effort.
Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...