...

View Full Version : Names of Images



rah111
07-06-2002, 01:55 AM
Hello everyone.

I want to be able to access the names of images which could be variably defined. That is, I won't know going in how many there will be or which ones. I have the following code:

<html>
<body>
This is an image reference test
<br>
<img src="gifimage.gif" name=im1>
<img src="jpgimage.jpg" name=im2>
<script language="javascript">
document.write(document.images["im1"])
document.write(document.images[1])
</script>
</body>
</html>

All I'm trying to do is display the name of each image on the page, but what I get is [object][object] instead.

Is there something I'm doing syntactically wrong here?

I was hoping to see "gifimage.gif" and "jpgimage.jpg".

Thanks in advance.

Russell.

ACJavascript
07-06-2002, 03:16 AM
you could do this

--------

<html>
<body>
This is an image reference test
<br>
<img src="gifimage.gif" name=im1>
<img src="jpgimage.jpg" name=im2>
<script language="javascript">
document.write(document.im1.src)
document.write("<BR>")
document.write(document.im2.src)
</script>
</body>
</html>

---------

Hope this helps :D

mordred
07-06-2002, 04:41 PM
Originally posted by rah111
All I'm trying to do is display the name of each image on the page, but what I get is [object][object] instead.


That is correct behaviour of javascript, you just print out the references to some document.images objects.



Is there something I'm doing syntactically wrong here?

I was hoping to see "gifimage.gif" and "jpgimage.jpg".


Not quite "wrong", you just missed something. See, document.images is a collection of objects. Each of these objects has also properties. Compare that to a car e.g.. The car is the object, and a property of it would be color, price, maximum speed etc. Ok so far? Now imagine that the car has also a name. If you want to get that name, you'd ask like

"what is the name or the second car".

In javascript, we could code that like

document.images[1].name

In your example, if you do

document.write(document.images[1].name);

it would print "im2". Why? Because "name" is defined by the attribute "name" in the <img> tag. You rather wanted to get it's filename, or speaking more technically, the value of it's src attribute, so you have to retrieve

document.write(document.images[1].src);

See how the individual attributes of a tag correspond to properties of an images object?
Putting it all together, since document.images is an object that behaves like an array, you can print out the "filename" of each image by using this snippet:

for (var i = 0; i < document.images.length; i++) {
document.write(document.images[i].src + "<br />");
}

premshree
07-06-2002, 08:44 PM
Here's the script:

<img src="do.gif" name="img1">

<script language="JavaScript">
function showImgName(imgID) // The main function
{
document.write(document[imgID].src.split("/")[document[imgID].src.split("/").length-1]);
}
showImgName("img1"); // Call the function wherever you want
</script>

:thumbsup:

rah111
07-07-2002, 12:50 AM
Thanks ACJavascript and Mordred.

Now I get it.

Particularly like the loop. That's just what I wanted to understand.

Russell.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum